Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!apple!veritas!amdcad!dgcad!dg-rtp!hunt From: hunt@dg-rtp.rtp.dg.com (Greg Hunt) Newsgroups: comp.unix.questions Subject: Re: changing a file Message-ID: <1991May21.211717.17685@dg-rtp.dg.com> Date: 21 May 91 21:17:17 GMT References: <91141.140213KJB6@psuvm.psu.edu> Sender: hunt@hobbit.rtp.dg.com (Greg Hunt) Reply-To: hunt@dg-rtp.rtp.dg.com Organization: Data General Corp., Research Triangle Park, NC Lines: 42 In article <91141.140213KJB6@psuvm.psu.edu>, KJB6@psuvm.psu.edu writes: > > As a script, I would like to search a file and replace a string with > another string. I know this can be done, but I have not figure out how to > do it yet. Anyone have any suggestions. I think if I use awk or nawk > somewhere > along the line, I can get it done. > > Any suggestions would be appreciated. Ken, yes awk or nawk (new awk, which is better), can do the job. If it is a simple substitution, using sed might be easier, however. Try something like this: sed -e 's/old_string/new_string/g' old_file > new_file This will change every occurrence of "old_string" to "new_string" in the file. The 's' is the substitute command, and the 'g' suffix says to change all occurrences. Note that you can't actually change the existing file, you get a new file. To get the new_file renamed so that you'll be left with only a file that contains the modifications, but has the original file name, do this after the sed: mv new_file old_file Take a look at the man page for sed for more examples. The commands look much like regular expressions you might use in searches in vi. Also take a look at the man page for awk for much more sophisticated processing that you can do using a C like programming language. Try: man sed | more man awk | more Enjoy! -- Greg Hunt Internet: hunt@dg-rtp.rtp.dg.com DG/UX Kernel Development UUCP: {world}!mcnc!rti!dg-rtp!hunt Data General Corporation Research Triangle Park, NC, USA These opinions are mine, not DG's.