changing a file
Greg Hunt
hunt at dg-rtp.rtp.dg.com
Wed May 22 07:17:17 AEST 1991
In article <91141.140213KJB6 at psuvm.psu.edu>, KJB6 at 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 at 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.
More information about the Comp.unix.questions
mailing list