From: Christian Brabandt on
On 2010-03-21, Hongyi Zhao <hongyi.zhao(a)gmail.com> wrote:
> I want to a insert some stuff as a new line after the line matching
> regexp. Any hints?

Use sed:

#v+
chrisbra(a)256bit:~$ echo -e "foobar\none\ntwo" |sed -e '/foobar/a\
> baz
> '
foobar
baz
one
two
#v-

regards,
Christian
From: Janis Papanagnou on
Hongyi Zhao wrote:
> Hi all,
>
> I want to a insert some stuff as a new line after the line matching
> regexp. Any hints?

awk '{print} /regexp/{print "some stuff"}'


Janis
From: Janis Papanagnou on
Hongyi Zhao wrote:
> On Sun, 21 Mar 2010 09:16:53 +0100, Janis Papanagnou
> <janis_papanagnou(a)hotmail.com> wrote:
>
>>> I want to a insert some stuff as a new line after the line matching
>>> regexp. Any hints?
>> awk '{print} /regexp/{print "some stuff"}'
>
> Thanks a lot. After careful consideration, my requirement should be
> described as follows:

Aha. Once again we're iterating to the task in question by multiple
postings, with still vague and incomplete requirements.

>
> 1- If "some stuff" already exits immediately after the line matching
> regexp, do nothing.

Can any of those patterns appear twice in the text?

>
> 2- If "some stuff" already exits in other locations, move it to
> desired location, i.e., immediately after the line matching regexp.

Also if such line(s) - can there be more than one? - exists more than
one line after the matching line - i.e. do you want two-pass processing?

>
> 3- If "some stuff" doesn't exit, insert it immediately after the line
> matching regexp.
>
> Best regards.

This task sounds familiar; as if we have already answered exactly that
question - probably even to exactly the same poster - a few weeks ago
(either here in c.u.s or in c.l.a). Please look it up.

Janis
From: Stephane CHAZELAS on
2010-03-21, 16:36(+08), Hongyi Zhao:
> On Sun, 21 Mar 2010 09:16:53 +0100, Janis Papanagnou
> <janis_papanagnou(a)hotmail.com> wrote:
>
>>> I want to a insert some stuff as a new line after the line matching
>>> regexp. Any hints?
>>
>> awk '{print} /regexp/{print "some stuff"}'
>
> Thanks a lot. After careful consideration, my requirement should be
> described as follows:
>
> 1- If "some stuff" already exits immediately after the line matching
> regexp, do nothing.
>
> 2- If "some stuff" already exits in other locations, move it to
> desired location, i.e., immediately after the line matching regexp.
>
> 3- If "some stuff" doesn't exit, insert it immediately after the line
> matching regexp.


awk '$0 != "some stuff"; /regexp/ {print "some stuff"}'

--
Stéphane
From: Ed Morton on
On 3/21/2010 3:36 AM, Hongyi Zhao wrote:
> On Sun, 21 Mar 2010 09:16:53 +0100, Janis Papanagnou
> <janis_papanagnou(a)hotmail.com> wrote:
>
>>> I want to a insert some stuff as a new line after the line matching
>>> regexp. Any hints?
>>
>> awk '{print} /regexp/{print "some stuff"}'
>
> Thanks a lot. After careful consideration, my requirement should be
> described as follows:
>
> 1- If "some stuff" already exits immediately after the line matching
> regexp, do nothing.
>
> 2- If "some stuff" already exits in other locations, move it to
> desired location, i.e., immediately after the line matching regexp.

What does that mean? You're already adding "some stuff" after the regexp line so
does "move it if it appears elsewhere" really mean "delete it if it appears
elsewhere" or are you trying to say that if a line CONTAINING "some stuff"
exists elsewhere you need to move that whole line to just after a line
containing regexp? If that's the case, which line containing regexp do you move
the "some stuff" line after, one before it or one after it or some random one?
Maybe some small sample input and the output you'd like given that input would
be useful.

Ed.

> 3- If "some stuff" doesn't exit, insert it immediately after the line
> matching regexp.
>
> Best regards.