nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Replacing text in a file using awk

This is a discussion on Replacing text in a file using awk within the Shell scripting forums, part of the Development/Scripting category; Hey guys, i am about to throw it all in because i just cannot get this to work :S. 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-30-2005, 08:40 AM
Junior Member
 
Join Date: Apr 2005
Posts: 3
Rep Power: 0
postyrus
Send a message via MSN to postyrus Send a message via Yahoo to postyrus
Default Replacing text in a file using awk

Hey guys, i am about to throw it all in because i just cannot get this to work :S.
I am currently using fedora core 3.

All i wanted to do is search through a file and find a string and replace with an argument from the command line.

So basically i have a file which has the contents of :

this is a test file
yes i am cool

say if i want to replace cool with $1 (which is 'crap', an argument from the shell) how do i accomplish this?

So far i have $1 = awk '/yes/ { print $4 }' file > file

I then want to overwrite the file with the updated contents. So the contents of file look like this:

this is a test file
yes i am crap

This just doesnt seem to do the job. If anyone knows please help!! i wont have any fingernails left soon :P thanks guys.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-30-2005, 02:41 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

I’m not sure whether you are using correct tool or not… IMPO you should try sed which has syntax like :

's/TEXT-TO-FIND/REPLACE-TEXT /g'


So to replace text in file i.e. cool with crap
Code:
sed -e 's/cool/crap/g' original.txt > new.txt
This should solve your problem… now you can write small script to do rest of work
Reply With Quote
  #3 (permalink)  
Old 05-01-2005, 09:13 AM
Junior Member
 
Join Date: Apr 2005
Posts: 3
Rep Power: 0
postyrus
Send a message via MSN to postyrus Send a message via Yahoo to postyrus
Default

Yeah thanks for your reply. I was thinking about using sed the other night and found out it was far easier to do what i was after than awk. However i have run into another problem.

How do i replace for example:

BIAS UD=34 UG=20

so if i wanna replace UG=20 with UG=30 i can use sed 's/UG=20/UG=30/'' ...easy.

But i am incrementing a UG from the command line so i need to change the UG occurrence already in the file with my new one ...eg:

INC=40

sed 's/UG=*/UG='$INC'/' i need to be able to change UG=* in the file where star is any value...is there anyway to take star as wildcard instead of literally? sorry for my poor english. Its hard to explain my problem.
Reply With Quote
  #4 (permalink)  
Old 05-02-2005, 02:46 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

Few days back i had read something as follows posted by vivek (old forum) which basically changes the values of the config file

Code:
CF="/etc/test.config" 
mod(){ 
NCF="$CF.$$" 
[ ! -f $CF ] && touch $CF || : 
V="$1" 
grep -v -E "^$V=" $CF > $NCF 
mv -f $NCF $CF 
eval echo "$V=\"'\${$V}'\"">> $CF 

} 

# test data
host='me.test.com' 
mod host 
mod ip
Save file and Execute

It will create /etc/test.config as follows
host='me.test.com'
ip=''

Again if you modify the script file with test data
host='YOU.test.com'

And script executed now output will be changed to as follows
host='YOU.test.com'
ip=''"

Try this one or may be we can play more with sed .... later ..
Reply With Quote
  #5 (permalink)  
Old 05-02-2005, 04:31 PM
Junior Member
 
Join Date: Apr 2005
Posts: 3
Rep Power: 0
postyrus
Send a message via MSN to postyrus Send a message via Yahoo to postyrus
Default

Thanks once again for your help. That is another way to accomplish the problem that i was having. However i have worked out a simple solution.

Where i was trying to substitute UG=* with UG="$INC" etc.....within shell i found you can use .* for all occurrences so i used UG=.*/UG="$INC" and presto....she works

I have finally got the program working 100 % with error checking. thanks heaps
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
text output to mysql chebbab Shell scripting 4 11-26-2007 08:29 PM
Cups is not printing text file with proper format, staircasing issue Ashish Pathak CentOS / RHEL / Fedora 1 11-18-2007 09:12 AM
rearranging columns in a text file sureshbup Shell scripting 2 12-06-2006 10:43 AM
Appending text files sparky Shell scripting 13 04-03-2006 05:43 PM


All times are GMT +5.5. The time now is 08:55 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