Just in case you don't have gnu said around:

http://sed.sourceforge.net/grabbag/tutorials/sedfaq.txt


4.11. How do I match only the first occurrence of a pattern?

   (1) The general solution is to use GNU sed or ssed, with one of
   these range expressions. The first script ("print only the first
   match") works with any version of sed:

     sed -n '/RE/{p;q;}' file       # print only the first match
     sed '0,/RE/{//d;}' file        # delete only the first match
     sed '0,/RE/s//to_that/' file   # change only the first match

   (2) If you cannot use GNU sed and if you *know* the pattern will
   not occur on the first line, this will work:

     sed '1,/RE/{//d;}' file        # delete only the first match
     sed '1,/RE/s//to_that/' file   # change only the first match

   (3) If you cannot use GNU sed and the pattern *might* occur on the
   first line, use one of the following commands (credit for short GNU
   script goes to Donald Bruce Stewart):

     sed '/RE/{x;/Y/!{s/^/Y/;h;d;};x;}' file       # delete (one way)
     sed -e '/RE/{d;:a' -e '$!N;$ba' -e '}' file   # delete (another way)
     sed '/RE/{d;:a;N;$ba;}' file                  # same script, GNU sed
     sed -e '/RE/{s//to_that/;:a' -e '$!N;$!ba' -e '}' file  # change

   Still another solution, using a flag in the hold space. This is
   portable to all seds and works if the pattern is on the first line:

     # sed script to change "foo" to "bar" only on the first occurrence
     1{x;s/^/first/;x;}
     1,/foo/{x;/first/s///;x;s/foo/bar/;}
     #---end of script---



"Edmund R. MacKenty" <[EMAIL PROTECTED]>
Sent by: Linux on 390 Port <[email protected]>
10/25/2006 04:17 PM
Please respond to
Linux on 390 Port <[email protected]>


To
[email protected]
cc

Subject
Re: [LINUX-390] use sed or awk or ?






On Wednesday 25 October 2006 15:57, Romanowski, John (OFT) wrote:
>I want to find the first comment line that begins with a target string
>in column 1 (#target) and replace only that first target line with
>another string.
>There are multiple lines that begin with #target.
>
>I've struck out with sed (not that I know sed).
>
>Any quick hints on a sed or awk or ?? sequence that does that?

Using GNU sed:
    sed '0,/^#target/s/^#target.*/#.../' file

This uses the "0,addr2" GNU extension to sed so that the substitute
command
will only be executed on lines up to and including the first line that
matches /^#target/.  Because the substitute command also contains the
pattern
match, only one line in that range (the last one, which is the first one
containing #target) will be changed.  Note that the standard sed "1,addr"
would change two lines if #target is on the first line, but using this GNU
extension fixes that.

Using awk:
    awk '/^#target/ && s == 0 {print "#..."; s = 1; next} {print}' file

This is more direct, because awk provides variables so it is easier to
maintain some state.  Basically it says if we find the #target line and we
haven't found it before, print out something else and remember that we did
that.  Otherwise, print out the line unchanged.

I'd recommend going with the awk command, because it is more portable and
easier for others to understand.
                 - MacK.
-----
Edmund R. MacKenty
Software Architect
Rocket Software, Inc.
Newton, MA USA

----------------------------------------------------------------------
For LINUX-390 subscribe / signoff / archive access instructions,
send email to [EMAIL PROTECTED] with the message: INFO LINUX-390 or
visit
http://www.marist.edu/htbin/wlvindex?LINUX-390


----------------------------------------------------------------------
For LINUX-390 subscribe / signoff / archive access instructions,
send email to [EMAIL PROTECTED] with the message: INFO LINUX-390 or visit
http://www.marist.edu/htbin/wlvindex?LINUX-390

Reply via email to