Path: utzoo!attcan!uunet!kddlab!cs.titech!wnoc-tyo-news!astemgw!icspub!rdmei!ptimtc!mips!swrinde!elroy.jpl.nasa.gov!usc!wuarchive!udel!princeton!phoenix.Princeton.EDU!tty!keyboard!subbarao!kartik From: subbarao@phoenix.Princeton.EDU (Kartik Subbarao) Newsgroups: comp.unix.shell Subject: Re: search and replace string from a script Message-ID: Date: 23 Apr 91 21:52:26 GMT References: <1991Apr23.180034.7349@progress.com> Sender: news@idunno.Princeton.EDU Reply-To: subbarao@phoenix (Kartik Subbarao) Lines: 27 In article <1991Apr23.180034.7349@progress.com> root@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@phoenix.Princeton.EDU -| Internet kartik@silvertone.Princeton.EDU (NeXT mail) SUBBARAO@PUCC.BITNET - Bitnet