User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 397,616 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,416 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser:
Views: 18719 | Replies: 3
Reply
Join Date: Apr 2005
Posts: 3
Reputation: ShaneUniStudent is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ShaneUniStudent ShaneUniStudent is offline Offline
Newbie Poster

Replacing text within a file using awk

  #1  
Apr 29th, 2005
Hey guys, i am about to throw it all in because i just cannot get this to work .

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 thanks guys.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Posts: 21
Reputation: vgersh99 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
vgersh99's Avatar
vgersh99 vgersh99 is offline Offline
Newbie Poster

Re: Replacing text within a file using awk

  #2  
May 2nd, 2005
/bin/echo "%s/cool/$1/g\nwq!" | ex -s file
vlad
+-----------------------------------+
| #include <disclaimer.h> |
+-----------------------------------+
Reply With Quote  
Join Date: Apr 2007
Posts: 1
Reputation: varkey is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
varkey varkey is offline Offline
Newbie Poster

Re: Replacing text within a file using awk

  #3  
Apr 14th, 2007
in awk it divides the line into fields .. so u can seperate yes i am cool in such a way that $1 represents yes,$2 for I ,$3 for am and $4 for cool...

/bin/echo "%s/cool/$1/g\nwq!" | ex -s file
Reply With Quote  
Join Date: Apr 2006
Posts: 137
Reputation: ghostdog74 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 26
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Replacing text within a file using awk

  #4  
Apr 14th, 2007
#!/usr/bin/bash
arg=$1
awk -v var=$arg '{  gsub(/yes/,var,$0); print  }' file
output:
#./test.sh
this is a test file
crap i am cool
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

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

 

DaniWeb Shell Scripting Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 8:05 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC