🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.lang.awk
Article #37

Re: How do I skip first match??

#37
From: tt
Date: Thu, 31 May 2001 09:39
17 lines
1795 bytes
"essbee" <spare_brain@yahoo.com> writes:

> Newbie help required for awk in skipping the first match.
> 
> I have a file that has two occurrences of "while(1)". I need to skip the
> first one and replace the second one with "while (x>MAXLOOP)" - how can I
> accomplish this?

awk '/while\(1\)/ && count++ { sub("while\(1\)","while(x>MAXLOOP)"}'

That will replace all occurrences except the first.
If you want to replace only the second (and skip possible 3rd &c), do

awk '/while\(1\)/ && ++count==2 ... '

-- 
Tapani Tarvainen
Technical Details
Message-ID: <n6snhlc0x3.fsf@xii5.it.jyu.fi>
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!dispose.news.demon.net!demon!easynet-monga!easynet.net!news1.ebone.net!news.ebone.net!newsrouter.euroconnect.net!news.clinet.fi!news.cs.hut.fi!newsfeed3.funet.fi!newsfeeds.funet.fi!mordred.cc.jyu.fi!not-for-mail
References: <kmkR6.54300$t12.4285836@bgtnsc05-news.ops.worldnet.att.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10ad19,8420015e5e3d2662
X-Google-Attributes: gid10ad19,public
X-Google-Thread: 109023,8420015e5e3d2662
X-Google-Attributes: gid109023,public
X-Google-Thread: 116030,8420015e5e3d2662
X-Google-Attributes: gid116030,public
X-Google-ArrivalTime: 2001-05-31 02:45:03 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!dispose.news.demon.net!demon!easynet-monga!easynet.net!news1.ebone.net!news.ebone.net!newsrouter.euroconnect.net!news.clinet.fi!news.cs.hut.fi!newsfeed3.funet.fi!newsfeeds.funet.fi!mordred.cc.jyu.fi!not-for-mail
From: tt@mit.jyu.fi
Newsgroups: alt.lang.awk,comp.lang.awk,fj.comp.lang.awk
Subject: Re: How do I skip first match??
Date: 31 May 2001 12:39:36 +0300
Organization: University of Jyvaskyla, Finland
Lines: 17
Message-ID: <n6snhlc0x3.fsf@xii5.it.jyu.fi>
References: <kmkR6.54300$t12.4285836@bgtnsc05-news.ops.worldnet.att.net>
NNTP-Posting-Host: xii5.it.jyu.fi
X-Trace: mordred.cc.jyu.fi 991302042 19795 130.234.160.155 (31 May 2001 09:40:42 GMT)
X-Complaints-To: newsmaster@cc.jyu.fi
NNTP-Posting-Date: Thu, 31 May 2001 09:40:42 +0000 (UTC)
X-Newsreader: Gnus v5.7/Emacs 20.5