nixCraft Linux Forum

nixCraft

Linux / UNIX 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

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 30-04-2005, 07:40 AM
Junior Member
User
 
Join Date: Apr 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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
  #2 (permalink)  
Old 30-04-2005, 01:41 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
OS: Debian GNU/Linux
Posts: 506
Thanks: 0
Thanked 8 Times in 6 Posts
Rep Power: 7
monk has a spectacular aura about monk has a spectacular aura about
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 01-05-2005, 08:13 AM
Junior Member
User
 
Join Date: Apr 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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 02-05-2005, 01:46 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
OS: Debian GNU/Linux
Posts: 506
Thanks: 0
Thanked 8 Times in 6 Posts
Rep Power: 7
monk has a spectacular aura about monk has a spectacular aura about
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 02-05-2005, 03:31 PM
Junior Member
User
 
Join Date: Apr 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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


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 Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
To find and replace in text file nashtech Shell scripting 5 31-01-2010 06:32 AM
text output to mysql chebbab Shell scripting 4 26-11-2007 07:29 PM
Cups is not printing text file with proper format, staircasing issue Ashish Pathak CentOS / RHEL / Fedora 1 18-11-2007 08:12 AM
rearranging columns in a text file sureshbup Shell scripting 2 06-12-2006 09:43 AM
Appending text files sparky Shell scripting 13 03-04-2006 04:43 PM


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


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

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 37 38