sed insert

  • Follow


hi,

I want to do something like this with sed but only on the first
occurrence of WORD:

#!/bin/sh
sed '
/WORD/ i\
Add this line before every line with WORD
'

thanks
ivan
0
Reply Lola 2/22/2011 4:45:55 PM

On Feb 22, 10:45=A0am, Lola <ivandipr...@gmail.com> wrote:
> hi,
>
> I want to do something like this with sed but only on the first
> occurrence of WORD:
>
> #!/bin/sh
> sed '
> /WORD/ i\
> Add this line before every line with WORD
> '
>
> thanks
> ivan

This NG is comp.lang.AWK:

   awk '/WORD/ && !found { print "This line came before first WORD";
found=3D1 }1' file

If you'd really prefer a cryptic, pointless sed solution you should
try comp.unix.shell.

Regards,

     Ed.
0
Reply Ed 2/22/2011 5:33:13 PM


In article <d7165fd7-4bbe-49bf-89ad-df10b5d7cef4@a21g2000prj.googlegroups.com>,
Ed Morton  <mortonspam@gmail.com> wrote:
....
>If you'd really prefer a cryptic, pointless sed solution you should
>try comp.unix.shell.

Bravo, Ed!

-- 
One of the best lines I've heard lately:

    Obama could cure cancer tomorrow, and the Republicans would be
    complaining that he had ruined the pharmaceutical business.

(Heard on Stephanie Miller = but the sad thing is that there is an awful lot
of direct truth in it.  We've constructed an economy in which eliminating
cancer would be a horrible disaster.  There are many other such examples.)
0
Reply gazelle 2/22/2011 5:35:37 PM

Tue, 22 Feb 2011 08:45:55 -0800, Lola did cat :

> hi,
> 
> I want to do something like this with sed but only on the first
> occurrence of WORD:
> 
> #!/bin/sh
> sed '
> /WORD/ i\
> Add this line before every line with WORD '
> 
> thanks
> ivan

It may seem a bit strange to ask about sed in an awk group
though as "sed and awk" an often heard mantra here's a possible answer, 
would that fit?:

$ seq 12 3 33 | sed '/^2/iAdd this line before every line beginning with 2'
12
15
18
Add this line before every line beginning with 2
21
Add this line before every line beginning with 2
24
Add this line before every line beginning with 2
27
30
33
0
Reply Loki 2/22/2011 5:40:38 PM

On Feb 22, 11:40=A0am, Loki Harfagr <l...@thedarkdesign.free.fr.INVALID>
wrote:
> Tue, 22 Feb 2011 08:45:55 -0800, Lola did cat=A0:
>
> > hi,
>
> > I want to do something like this with sed but only on the first
> > occurrence of WORD:
>
> > #!/bin/sh
> > sed '
> > /WORD/ i\
> > Add this line before every line with WORD '
>
> > thanks
> > ivan
>
> It may seem a bit strange to ask about sed in an awk group
> though as "sed and awk" an often heard mantra here's a possible answer,
> would that fit?:
>
> $ seq 12 3 33 | sed '/^2/iAdd this line before every line beginning with =
2'
> 12
> 15
> 18
> Add this line before every line beginning with 2
> 21
> Add this line before every line beginning with 2
> 24
> Add this line before every line beginning with 2
> 27
> 30
> 33

That's the solution the OP already had, (s)he wanted a different
solution that'd only insert a line on the FIRST occurrence of the
pattern in the file.

     Ed.
0
Reply Ed 2/22/2011 8:30:05 PM

Tue, 22 Feb 2011 12:30:05 -0800, Ed Morton did cat :

> On Feb 22, 11:40 am, Loki Harfagr <l...@thedarkdesign.free.fr.INVALID>
> wrote:
>> Tue, 22 Feb 2011 08:45:55 -0800, Lola did cat :
>>
>> > hi,
>>
>> > I want to do something like this with sed but only on the first
>> > occurrence of WORD:
>>
>> > #!/bin/sh
>> > sed '
>> > /WORD/ i\
>> > Add this line before every line with WORD '
>>
>> > thanks
>> > ivan
>>
>> It may seem a bit strange to ask about sed in an awk group though as
>> "sed and awk" an often heard mantra here's a possible answer, would
>> that fit?:
>>
>> $ seq 12 3 33 | sed '/^2/iAdd this line before every line beginning
>> with 2' 12
>> 15
>> 18
>> Add this line before every line beginning with 2 21
>> Add this line before every line beginning with 2 24
>> Add this line before every line beginning with 2 27
>> 30
>> 33
> 
> That's the solution the OP already had, (s)he wanted a different

Ooops ! Oh well yes indeed! Er.. Aughhh!
You're right! I read the OP with my left foot, probably abused by the
"before every line" forcing the context to my tired toes ;-)

> solution that'd only insert a line on the FIRST occurrence of the
> pattern in the file.
> 
>      Ed.

Ah, well, then that'd be some variation on this:
-------
  seq 12 3 33  | sed  '1{x;s/^$/HOTSTUFF/;x} ;  /^2/{x ; x;s/^$//g; x}' 
12
15
18
HOTSTUFF
21
24
30
33
-------

and then you're most probably right for a second time as you posted
a good solution in awk while soothing about "cryptic, pointless sed solution" ;D)
0
Reply Loki 2/22/2011 11:23:51 PM

On Feb 22, 5:33=A0pm, Ed Morton <mortons...@gmail.com> wrote:

> This NG is comp.lang.AWK:

True, but the "sed & awk" book tends to associate them in people's
minds.

> =A0 =A0awk '/WORD/ && !found { print "This line came before first WORD";
> found=3D1 }1' file
>
> If you'd really prefer a cryptic, pointless sed solution you should
> try comp.unix.shell.

In this case and most others, I would agree. There are very few
reasons to use sed instead of awk.

-Ed

0
Reply Edward 2/23/2011 5:54:47 PM

On Feb 23, 11:54=A0am, Edward Rosten <edward.ros...@gmail.com> wrote:
> On Feb 22, 5:33=A0pm, Ed Morton <mortons...@gmail.com> wrote:
>
> > This NG is comp.lang.AWK:
>
> True, but the "sed & awk" book tends to associate them in people's
> minds.

Odd that people don't get confused about the difference between mice &
men though since that associations been made in 2 books I can think
of, and at least one of those is EVEN more famous than "sed &
awk".... :-).

>
> > =A0 =A0awk '/WORD/ && !found { print "This line came before first WORD"=
;
> > found=3D1 }1' file
>
> > If you'd really prefer a cryptic, pointless sed solution you should
> > try comp.unix.shell.
>
> In this case and most others, I would agree. There are very few
> reasons to use sed instead of awk.

I actually do use sed fairly frequently but only for what it's good at
- simple substitutions on a single line.

       Ed.
0
Reply Ed 2/23/2011 6:47:25 PM

In article <3fba8a20-27f8-4c43-b76b-5a058e828754@f36g2000pri.googlegroups.com>,
Ed Morton  <mortonspam@gmail.com> wrote:
>On Feb 23, 11:54�am, Edward Rosten <edward.ros...@gmail.com> wrote:
>> On Feb 22, 5:33�pm, Ed Morton <mortons...@gmail.com> wrote:
>>
>> > This NG is comp.lang.AWK:
>>
>> True, but the "sed & awk" book tends to associate them in people's
>> minds.
>
>Odd that people don't get confused about the difference between mice &
>men though since that associations been made in 2 books I can think
>of, and at least one of those is EVEN more famous than "sed &
>awk".... :-).

Bravo, Ed!  (Again!)

The point is that I have never understood why a reference to an O'Reilly
book was seen as a defense in a Usenet topicality war.  Granted, it is
probably safe to say that many or most of the posters to comp.lang.awk
have, at one time or another in their lives, read an O'Reilly book;
however, that doesn't mean that O'Reilly books have any special standing
here.
....

>I actually do use sed fairly frequently but only for what it's good at
>- simple substitutions on a single line.

Exactly right.  Me, too.

-- 
But the Bush apologists hope that you won't remember all that. And they
also have a theory, which I've been hearing more and more - namely,
that President Obama, though not yet in office or even elected, caused the
2008 slump. You see, people were worried in advance about his future
policies, and that's what caused the economy to tank. Seriously.

    (Paul Krugman - Addicted to Bush)

0
Reply gazelle 2/25/2011 5:35:31 PM

On Feb 25, 5:35=A0pm, gaze...@shell.xmission.com (Kenny McCormack)
wrote:

> >Odd that people don't get confused about the difference between mice &
> >men though since that associations been made in 2 books I can think
> >of, and at least one of those is EVEN more famous than "sed &
> >awk".... :-).
>
> Bravo, Ed! =A0(Again!)
>
> The point is that I have never understood why a reference to an O'Reilly
> book was seen as a defense in a Usenet topicality war. =A0Granted, it is
> probably safe to say that many or most of the posters to comp.lang.awk
> have, at one time or another in their lives, read an O'Reilly book;
> however, that doesn't mean that O'Reilly books have any special standing
> here.
> ...

Not that I agree with it, but playing devil's advocate...

Sed and AWK are rather closely related in programming model (well, the
pattern/action style), and are used for similar sorts of tasks (up to
the point where it is no longer worth using sed). That, coupled with a
rather well-known book on the topic tends to associate them in
people's mind. The difference between the two books is that "sed and
awk" are both about quite closely related stream editors, whereas "of
mice and men" isn't (for instance) a comparative study two mammals.


> >I actually do use sed fairly frequently but only for what it's good at
> >- simple substitutions on a single line.
>
> Exactly right. =A0Me, too.

As do I.

-Ed


0
Reply Edward 3/1/2011 11:24:52 AM

In article <f6a1b9db-bac0-4031-aec3-1bf013de2f4d@v31g2000vbs.googlegroups.com>,
Edward Rosten  <edward.rosten@gmail.com> wrote:
....
>The difference between the two books is that "sed and
>awk" are both about quite closely related stream editors, whereas "of
>mice and men" isn't (for instance) a comparative study two mammals.

Bravo, Ed (yet again!)

A definite LOL was had by all.

-- 

Some of the more common characteristics of Asperger syndrome include: 

* Inability to think in abstract ways (eg: puns, jokes, sarcasm, etc)
* Difficulties in empathising with others
* Problems with understanding another person's point of view
* Hampered conversational ability
* Problems with controlling feelings such as anger, depression 
    and anxiety
* Adherence to routines and schedules, and stress if expected routine 
    is disrupted
* Inability to manage appropriate social conduct
* Delayed understanding of sexual codes of conduct
* A narrow field of interests. For example a person with Asperger 
    syndrome may focus on learning all there is to know about 
    baseball statistics, politics or television shows.
* Anger and aggression when things do not happen as they want
* Sensitivity to criticism
* Eccentricity
* Behaviour varies from mildly unusual to quite aggressive 
    and difficult

0
Reply gazelle 3/1/2011 12:16:17 PM

On Mar 1, 5:24=A0am, Edward Rosten <edward.ros...@gmail.com> wrote:
> On Feb 25, 5:35=A0pm, gaze...@shell.xmission.com (Kenny McCormack)
> wrote:
>
> > >Odd that people don't get confused about the difference between mice &
> > >men though since that associations been made in 2 books I can think
> > >of, and at least one of those is EVEN more famous than "sed &
> > >awk".... :-).
>
> > Bravo, Ed! =A0(Again!)
>
> > The point is that I have never understood why a reference to an O'Reill=
y
> > book was seen as a defense in a Usenet topicality war. =A0Granted, it i=
s
> > probably safe to say that many or most of the posters to comp.lang.awk
> > have, at one time or another in their lives, read an O'Reilly book;
> > however, that doesn't mean that O'Reilly books have any special standin=
g
> > here.
> > ...
>
> Not that I agree with it, but playing devil's advocate...
>
> Sed and AWK are rather closely related in programming model (well, the
> pattern/action style), and are used for similar sorts of tasks (up to
> the point where it is no longer worth using sed). That, coupled with a
> rather well-known book on the topic tends to associate them in
> people's mind. The difference between the two books is that "sed and
> awk" are both about quite closely related stream editors, whereas "of
> mice and men" isn't (for instance) a comparative study two mammals.
>
> > >I actually do use sed fairly frequently but only for what it's good at
> > >- simple substitutions on a single line.
>
> > Exactly right. =A0Me, too.
>
> As do I.
>
> -Ed

Not that it matters, and I get where you're coming from (and I'm
certainly not suggesting anoyne actually do this in a posting to this
NG!) but I think I could name MANY more similarities between mice and
men than I could between sed and awk. Just some cheese for thought....

       Ed.
0
Reply Ed 3/1/2011 6:28:40 PM

11 Replies
477 Views

(page loaded in 0.117 seconds)

Similiar Articles:


















7/26/2012 11:33:48 PM


Reply: