Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text replacement within a file

Status
Not open for further replies.

smacattack

Technical User
Jun 25, 2001
102
GB
How do i use nawk to be able to pick out a set of characters within a file and replace them with a new statement.
e.g

For every occurance of "FRED 10" in a file i need to change it to "PETE 20"

Cannot anybody help i cannot get my head around nawk!?
 
Sorry, can't help with nawk, but you can do the same thing with sed. Something like:

/FRED 10/cPETE 20

will do the job, if placed in a file and run as:

sed -f [sedscript] [file to change] > newfile

Hope this helps.
 


Hi,
I wouldn't use NAWK, I would use SED since that is better at substitution.

PATTERN="FRED 10"
REPLACE="PETE 20"
sed -e "s#$PATTERN#$REPLACE#g" myfile > /tmp/myfile
cp /tmp/myfile myfile

 
This might work and it's simple


tr 'FRED 10' 'PETE 20' < file1> file2
 
how 'bout...

nawk 'gsub(&quot;fred&quot;,&quot;pete&quot;) {print}' inputfile

gsub stands for &quot;global substitute&quot;.

check the man pages for gsub
 
oops, slight mistake...

nawk 'gsub(&quot;FRED 10&quot;,&quot;PETE 20&quot;) {print}' inputfile

watch out... parenthases after gsub, curly brackets around print. don't get 'em confused :)

hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top