search and replace string from a script

Kartik Subbarao subbarao at phoenix.Princeton.EDU
Wed Apr 24 07:52:26 AEST 1991


In article <1991Apr23.180034.7349 at progress.com> root at progress.COM (Root of all Evil) writes:
>
>Is there any way within ex (or some other text processing utility) to
>access the nth occurrence of a pattern?  What I'd like to do is search
>a file for the nth occurrence of a pattern and then change that pattern
>but no others.  I've tried using ex:
>
>ex -s FILE << QUIT
>/STRING1/n s/STRING1/STRING2/
>wq!
>QUIT

If you have perl, you can say this:

% perl -pi -e 'if (/STRING1/) { s/STRING1/STRING2/ if (++$num == N); }' FILE

Isn't perl cool :-)

		-Kartik


--
internet# rm `df | tail +2 | awk '{ printf "%s/quotas\n",$6}'`

subbarao at phoenix.Princeton.EDU -| Internet
kartik at silvertone.Princeton.EDU (NeXT mail)  
SUBBARAO at PUCC.BITNET			          - Bitnet



More information about the Comp.unix.shell mailing list