nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Appending text files

This is a discussion on Appending text files within the Shell scripting forums, part of the Development/Scripting category; I am making my server script big and better but once again i have run into trouble with what i ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 04-01-2006, 08:02 PM
Member
User
 
Join Date: Mar 2006
Posts: 35
Rep Power: 0
sparky
Default Appending text files

I am making my server script big and better but once again i have run into trouble with what i thought would be the simplest parts of the script.
I want delete one line from a text file and replace it with new.
ie
delete password"*" from text file and add new password"$"
from what i have read the command > deletes all and adds new
and >> just adds new line to rest .
using google i have seems some thing on sed and awk so i assume i need to use one of them by maybe making a new file then appending then rename back to old but not quite sure how
Have tried this
sed -e 's/rcon_password "*"/rcon_password "foo"/g' server.cfg > server1.cfg

but the out is this
rcon_password "foo"origanpass"
so the wild card is not getting rid of old pass
any help would be great

Thanks
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-01-2006, 10:44 PM
tom tom is offline
Contributors
User
 
Join Date: Jun 2005
Location: London, UK
Posts: 213
Rep Power: 4
tom is on a distinguished road
Default

I think you want to do find and replace then use replace command:
Code:
replace "OLDTEXT" "NEWTEXT" <INPUT-FILE >OUTPUT-FILE
See more usage at
http://www.cyberciti.biz/nixcraft/vi...ng-in-many.php
Reply With Quote
  #3 (permalink)  
Old 04-01-2006, 11:55 PM
Member
User
 
Join Date: Mar 2006
Posts: 35
Rep Power: 0
sparky
Default

tried that but i get
-bash: replace: command not found
I checked out you link and the peal looked good so i tried that and it works same as sed
The problem i am having it that i dont no the password with out opening the text file
ie.. somewhere in the text file is the line password"foo"
so i need to replace password "*" with password "newpass"
but the wild card does not seem to work it keeps old pass and adds new one as well.
password "foo""newpass"

thanks for any help
Reply With Quote
  #4 (permalink)  
Old 04-02-2006, 08:49 AM
rockdalinux's Avatar
Contributors
User
 
Join Date: May 2005
Location: Bangalore
My distro: RHEL, HP-UX, Solaris, FreeBSD, Ubuntu
Posts: 581
Rep Power: 7
rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough
Default

Can you post your sample text/data file, highlighting password entry?
__________________
Rocky Jr.
You may have my body & soul, but you will never touch my pride!

If you have knowledge, let others light their candles at it.

Certified to work on HP-UX / Sun Solaris / RedHat
Reply With Quote
  #5 (permalink)  
Old 04-02-2006, 04:30 PM
Member
User
 
Join Date: Mar 2006
Posts: 35
Rep Power: 0
sparky
Default

what it is i am helping a friend run a counterstrike source dedicated servers
He ne not very comfortable using ssh so thought even though i am new to linux myself i would write so scripts to make the job easy the first on my last post mean't he could turn on any servers just be answering a few questions
this script will hopefully change to rcon password for each server

Code:
// server name
hostname "mtservers" 

rcon_password "foo"

// Server password
sv_password "edmatch" 

// server cvars
log 0
mp_friendlyfire 1 
mp_footsteps 1 
mp_autoteambalance 0 
mp_autokick 0 
mp_flashlight 0
i have tried
Code:
sed -e 's/rcon_password "*"/rcon_password "bar"/g' server.cfg > server1.cfg
and i get this
Code:
// server name
hostname "mtservers" 

rcon_password "bar"foo"

// Server password
sv_password "edmatch" 

// server cvars
log 0
mp_friendlyfire 1 
mp_footsteps 1 
mp_autoteambalance 0 
mp_autokick 0 
mp_flashlight 0
i.ve tried
Code:
 perl -p -i -e 's/rcon_password "*"/rcon_password "bar"/g' server.cfg
output
Code:
// server name
hostname "mtservers" 

rcon_password "bar"foo"

// Server password
sv_password "edmatch" 

// server cvars
log 0
mp_friendlyfire 1 
mp_footsteps 1 
mp_autoteambalance 0 
mp_autokick 0 
mp_flashlight 0
so as you can see can t get a wild card to work
From what ive read "" '' `` can cause u hasle may be that where i am going wrong
peal works best as i don t need to make new file and it works fine if i type the old pass but that defeats the object of the script.
Anyway thanks for any help
Reply With Quote
  #6 (permalink)  
Old 04-02-2006, 08:14 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,036
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Replace

Code:
sed -e 's/rcon_password "*"/rcon_password "bar"/g' server.cfg > server1.cfg
With
Code:
sed '/rcon_password/s/foo/bar/g' server.cfg >   server1.cfg
How it works?
Above sed will substitute "foo" with "bar" ONLY for lines which contain "rcon_password"
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #7 (permalink)  
Old 04-02-2006, 08:34 PM
Member
User
 
Join Date: Mar 2006
Posts: 35
Rep Power: 0
sparky
Default

Thanks that also works, But is there a way i can use a wild card with old pass
Reply With Quote
  #8 (permalink)  
Old 04-02-2006, 10:12 PM
Member
User
 
Join Date: Mar 2006
Posts: 35
Rep Power: 0
sparky
Default

Thought of work around for old pass but still stuck as now i can t use $ in my code he what ive got so far.
Code:
#dispaly old rcon pass
echo -n "The old " 
 cat server.cfg |grep rcon_pass*
echo
echo
#set oldpass and new pass vars
echo -n "Please enter old pass : "
read oldpass
echo
echo
echo -n "Please enter new pass : "
read newpass
#set pass
#perl -p -i -e 's/$oldpass/$newpass/g' server.cfg
sed '/rcon_password/s/$oldpass/$newpass/g' server.cfg >   server1.cfg
am trying perl commented out and sed peal seems best option if i can get that working as there would be no need to mv server1.cfg to server.cfg
But either would be fine

thanks
Reply With Quote
  #9 (permalink)  
Old 04-02-2006, 10:15 PM
Member
User
 
Join Date: Mar 2006
Posts: 35
Rep Power: 0
sparky
Default

Quote:
Originally Posted by sparky
Thought of work around for old pass but still stuck as now i can t use $ in my code he what ive got so far.
Code:
#dispaly old rcon pass
echo -n "The old " 
 cat server.cfg |grep rcon_pass*
echo
echo
#set oldpass and new pass vars
echo -n "Please enter old pass : "
read oldpass
echo
echo
echo -n "Please enter new pass : "
read newpass
#set pass
#perl -p -i -e 's/$oldpass/$newpass/g' server.cfg
sed '/rcon_password/s/$oldpass/$newpass/g' server.cfg >   server1.cfg
am trying perl commented out and sed
peal seems best option if i can get that working as there would be no need to mv server1.cfg to server.cfg
But either would be fine

thanks
Reply With Quote
  #10 (permalink)  
Old 04-03-2006, 12:18 AM
tom tom is offline
Contributors
User
 
Join Date: Jun 2005
Location: London, UK
Posts: 213
Rep Power: 4
tom is on a distinguished road
Default

Quote:
Thanks that also works, But is there a way i can use a wild card with old pass
What do you mean by wild card? If I am not mistaken it means you just want to setup new password without prompting for old password, am I right?
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
To find and replace in text file nashtech Shell scripting 3 07-31-2008 07:46 PM
need to convert text files into mysql database zafar466 Databases servers 2 07-12-2008 01:31 PM
text output to mysql chebbab Shell scripting 4 11-26-2007 08:29 PM
rearranging columns in a text file sureshbup Shell scripting 2 12-06-2006 10:43 AM
Replacing text in a file using awk postyrus Shell scripting 4 05-02-2005 04:31 PM


All times are GMT +5.5. The time now is 08:24 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36