Tags: astring, character, file, insert, instance, line, linux, programming, sed, seeking, single, statement, text, unix

Seeking help with Sed statement.

On Programmer » Unix & Linux

12,156 words with 9 Comments; publish: Wed, 30 Apr 2008 21:51:00 GMT; (20078.13, « »)

I need help with a sed statement.

The problem: Insert a single line of text before the first instance of a

string in a file. The first character of the string is always the same,

the rest, variable. There may be several lines of this string in the

variable-length file. Example:

a

b

c

d

+something

+anything

- I want to insert the line "Insert" before the first line beginning with a

plus. To my understanding of sed it would be:

cat file | sed -e '/^+/ i\Insert'

But that's not working. Note: I've been able to write the fix in awk,

but it's annoying me that I can't do it in sed.

Can anyone provide insight?

.........................................................

.

"War is an ugly thing, but not the ugliest of things; The decayed and

degraded state of moral and patriotic feelings which thinks that nothing

is worth war is much worse. A man who has nothing for which he is willing

to fight, nothing which is more important than his own personal safety,

is a miserable creature and has no chance of being free unless made and

kept so by the exertions of better men than himself"

- John Stuart Mill

.........................................................

.

dswan.unix-linux.todaysummary.com.m3m3t1ccand1ru.com [url]http://www.memeticcandiru.com[/url

]

All Comments

Leave a comment...

  • 9 Comments
    • On Sun, 15 May 2005 at 19:30 GMT, l3gl3$$_m4r1n3.unix-linux.todaysummary.com.salmahayeksknockers.edu wrote:

      > I need help with a sed statement.

      > The problem: Insert a single line of text before the first instance of a

      > string in a file. The first character of the string is always the same,

      > the rest, variable. There may be several lines of this string in the

      > variable-length file. Example:

      > a

      > b

      > c

      > d

      > +something

      > +anything

      > - I want to insert the line "Insert" before the first line beginning with

      a

      > plus. To my understanding of sed it would be:

      > cat file | sed -e '/^+/ i\Insert'

      > But that's not working. Note: I've been able to write the fix in awk,

      > but it's annoying me that I can't do it in sed.

      What does "not working" mean? What _exactly_ is the problem?

      That script works for me (though I don't use the unnecessary

      "cat").

      Chris F.A. Johnson <http://cfaj.freeshell.org>

      ========================================

      ==========================

      Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress

      <http://www.torfree.net/~chris/books/ssr.html>

      #1; Wed, 30 Apr 2008 21:53:00 GMT
    • l3gl3$$_m4r1n3.unix-linux.todaysummary.com.salmahayeksknockers.edu writes:

      l3gl3$$_m4r1n3.unix-linux.todaysummary.com.salmahayeksknockers.edu writes:

      > I need help with a sed statement.

      > The problem: Insert a single line of text before the first instance of a

      > string in a file. The first character of the string is always the same,

      > the rest, variable. There may be several lines of this string in the

      > variable-length file. Example:

      > a

      > b

      > c

      > d

      > +something

      > +anything

      > - I want to insert the line "Insert" before the first line beginning with

      a

      > plus. To my understanding of sed it would be:

      > cat file | sed -e '/^+/ i\Insert'

      > But that's not working. Note: I've been able to write the fix in awk,

      > but it's annoying me that I can't do it in sed.

      > Can anyone provide insight?

      cat /tmp/file| sed -e '/^+/{H

      i\

      Insert

      :copy

      n

      b copy

      }'

      or:

      cat /tmp/file| sed -e '/^+/{H' -e 'i\Insert' -e ':copy n' -e 'b copy }'

      __Pascal Bourguignon__ http://www.informatimago.com/

      Nobody can fix the economy. Nobody can be trusted with their finger

      on the button. Nobody's perfect. VOTE FOR NOBODY.

      #2; Wed, 30 Apr 2008 21:54:00 GMT
    • In comp.unix.shell Chris F.A. Johnson <cfajohnson.unix-linux.todaysummary.com.gmail.com> wrote:

      > What does "not working" mean? What _exactly_ is the problem?

      GNU sed under Linux:

      dswan:~$ cat /etc/passwd | sed '/^vicki/ i\Insert'

      sed: -e expression #1, char 12: Extra characters after command

      .........................................................

      .

      "Be wary of enraging a little man, for he will retaliate with the

      force of a hundred little men"

      -My Girlfriend, Lisa

      .........................................................

      .

      dswan.unix-linux.todaysummary.com.m3m3t1ccand1ru.com [url]http://www.memeticcandiru.com[/url

      ]

      #3; Wed, 30 Apr 2008 21:55:00 GMT
    • On Sun, 15 May 2005 at 23:16 GMT, l3gl3$$_m4r1n3.unix-linux.todaysummary.com.salmahayeksknockers.edu wrote:

      > In comp.unix.shell Chris F.A. Johnson <cfajohnson.unix-linux.todaysummary.com.gmail.com> wrote:

      >

      > GNU sed under Linux:

      > dswan:~$ cat /etc/passwd | sed '/^vicki/ i\Insert'

      > sed: -e expression #1, char 12: Extra characters after command

      Did you cut and paste the line or retype it?

      That works for me as is; but there's still no need for cat:

      sed '/^vicki/ i\Insert' /etc/passwd

      Chris F.A. Johnson <http://cfaj.freeshell.org>

      ========================================

      ==========================

      Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress

      <http://www.torfree.net/~chris/books/ssr.html>

      #4; Wed, 30 Apr 2008 21:56:00 GMT
    • In comp.unix.shell Chris F.A. Johnson <cfajohnson.unix-linux.todaysummary.com.gmail.com> wrote:

      > On Sun, 15 May 2005 at 23:16 GMT, l3gl3$$_m4r1n3.unix-linux.todaysummary.com.salmahayeksknockers.edu w

      rote:

      > Did you cut and paste the line or retype it?

      > That works for me as is; but there's still no need for cat:

      > sed '/^vicki/ i\Insert' /etc/passwd

      Hmmm.. It still didn't work for me, even after typing it in manually:

      dswan:~$ sed '/^vicki/ i\Insert' /etc/passwd

      sed: -e expression #1, char 12: Extra characters after command

      What OS are you using? Which sed? Mine's: GNU sed version 3.02

      .........................................................

      .

      The welfare of the people in particular has always been the alibi of

      tyrants, and it provides the further advantage of giving the servants of

      tyranny a good conscience.

      - Albert Camus

      .........................................................

      .

      dswan.unix-linux.todaysummary.com.m3m3t1ccand1ru.com [url]http://www.memeticcandiru.com[/url

      ]

      #5; Wed, 30 Apr 2008 21:57:00 GMT
    • On comp.unix.shell, in <txWhe.64133$HR1.28163.unix-linux.todaysummary.com.clgrps12>, "h41ry_p0073r.unix-linux.todaysummary.com.salmahayeksknockers.

      edu" wrote:

      >

      <snip>

      > --

      > ........................................................

      ..

      > OOO OOOOOOO OO OOO OOOOOO OO OOOOOOOOOO OOO OOOOOO OOOO OOO OOOOO OO

      > OOOOOOO, OOO OO OOOOOOOO OOO OOOOOOO OOOOOOOOO OO OOOOOO OOO OOOOOOOO OO

      > OOOOOOO O OOOO OOOOOOOOOO.

      > - OOOOOO OOOOO

      > ........................................................

      ..

      > OOOOO.unix-linux.todaysummary.com.OOOOOOOOOOOOOO.OOO OOOO://OOO.OOOOOOOOOOOOOO.OOO[/col

      or]

      <all letters and numbers above converted to "0">

      The Netiquette limits on a sig are 4 lines total, including blank

      lines, after a delimeter "-- " alone on a line.

      I didn't read your article to see what quote you like this w.

      I don't give a shit and I don't like having to read it.

      Sigs are supposed to be sigs, not bulletin boards.

      If you have something else that you want others to read/see, then

      put it in URL in a legal sig.

      Or, you will find your posts silently ignored and yourself often

      silently killfiled.

      I know the answer to your question too. If no one else had

      helped you, you would be out of luck.

      Many people who _could_ have answered your question, that

      monitor this group, did not.

      And Chris FAJ is just a XXXXing hypocrite: He'll freak if you

      top post or fail to trim your posts but ignores large sigs

      because _I_ don't let them pass and I have had the nerve to

      stand up to the would-be bully in the past.

      And the poor little chubby-wubby baby has never gotten over

      it.

      (and he doesn't know sed at all, obviously)

      Fix your sig or I'll killfile you, JERK.

      Get your XXXXing bulletin board out of my face or get out of my

      newsreader.

      Now, Bill Marcum, whom just tried to bully me too, will

      post something bitchy.

      Who cares. In real life I'd stomp his ass just like I

      did here.

      AC

      alanconnor AT earthlink DOT net

      Use your real return address or I'll never know you

      even tried to mail me. http://tinyurl.com/2t5kp

      #6; Wed, 30 Apr 2008 21:58:00 GMT
    • On Mon, 16 May 2005 at 05:51 GMT, h41ry_p0073r.unix-linux.todaysummary.com.salmahayeksknockers.edu wrote:

      > In comp.unix.shell Chris F.A. Johnson <cfajohnson.unix-linux.todaysummary.com.gmail.com> wrote:

      >

      >

      >

      > Hmmm.. It still didn't work for me, even after typing it in manually:

      Well, DON'T type it; cut and paste it.

      > dswan:~$ sed '/^vicki/ i\Insert' /etc/passwd

      > sed: -e expression #1, char 12: Extra characters after command

      > What OS are you using? Which sed? Mine's: GNU sed version 3.02

      Mine is GNU sed 4.1.1

      I tried 3.02 and got the same error as you. However this works (as

      I think someone else has already suggested):

      sed '/^vicki/i\

      Insert' /etc/passwd

      Howver, on rereading your original post, I see that you only

      wanted the insert before the first occurrence. For that, someone

      else gave the sed solution. It was, I thought, a good

      advertisement for awk.

      Chris F.A. Johnson <http://cfaj.freeshell.org>

      ========================================

      ==========================

      Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress

      <http://www.torfree.net/~chris/books/ssr.html>

      #7; Wed, 30 Apr 2008 21:59:00 GMT
    • In comp.unix.shell Alan Connor <zzzzzz.unix-linux.todaysummary.com.xxx.yyy> wrote:

      > Get your XXXXing bulletin board out of my face or get out of my

      > newsreader.

      > Now, Bill Marcum, whom just tried to bully me too, will

      > post something bitchy.

      > Who cares. In real life I'd stomp his ass just like I

      > did here.

      I am surprised that someone so focused on etiquette finds it acceptable to

      make threats of violence.

      Switch to decaf, dude.

      .........................................................

      .

      "Woe to him who builds his house by unrighteousnesswho makes his neighbour

      serve him for nothing, and does not give him wages"

      -Jeremiah 22:13

      .........................................................

      .

      dswan.unix-linux.todaysummary.com.m3m3t1ccand1ru.com [url]http://www.memeticcandiru.com[/url

      ]

      #8; Wed, 30 Apr 2008 22:00:00 GMT
    • c4rp3t_munch3r.unix-linux.todaysummary.com.salmahayeksknockers.edu writes:

      > In comp.unix.shell Alan Connor <zzzzzz.unix-linux.todaysummary.com.xxx.yyy> wrote:

      >

      >

      >

      > I am surprised that someone so focused on etiquette finds it acceptable to

      > make threats of violence.

      > Switch to decaf, dude.

      Alan Connor is a troll. Ignore him.

      Mns Rullgrd

      mru.unix-linux.todaysummary.com.inprovide.com

      #9; Wed, 30 Apr 2008 22:01:00 GMT