[comp.unix.questions] changing a file

KJB6@psuvm.psu.edu (05/22/91)

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

hunt@dg-rtp.rtp.dg.com (Greg Hunt) (05/22/91)

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.

tchrist@convex.COM (Tom Christiansen) (05/22/91)

From the keyboard of KJB6@psuvm.psu.edu:
:
: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.

I believe the easiest, most flexible, and most powerful solution would
be along these lines:

    perl -p -i.ORIG -e 's/old/new/g' file1 file2 ...

This will globally replace all instances of "old" with "new" in 
all listed files, keeping a backup copy in file1.ORIG, etc.  Omit
the ".old" part if you're brave enough.

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
		"So much mail, so little time." 

eed_wwhh@jhunix.HCF.JHU.EDU (William H Huggins) (05/24/91)

-- 
W.H. Huggins ECE Dept, Johns Hopkins University, Baltimore MD 21218
attmail!huggin!whh,  eed_wwhh@jhunix.hcf.jhu.edu
Home: 1 E Univ. Pkwy, Baltimore MD 21218; (301)8894780 voice.
 

jpr@jpradley.jpr.com (Jean-Pierre Radley) (05/31/91)

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.


The answer I'd provide is not my own, it's straight out of Kernighan & Pike.
Look in their book's index for 'replace'.

(They use sed for this, since awk would work but would be overkill.)

Jean-Pierre Radley   Unix in NYC   jpr@jpr.com   jpradley!jpr   CIS: 72160,1341