Re: Seeking help with Sed statement.

From: John Savage (rookswood_at_suburbian.com.au)
Date: 05/18/05


Date: Wed, 18 May 2005 00:47:56 GMT

l3gl3$$_m4r1n3@salmahayeksknockers.edu writes:
>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?

Your query prompted me to reinvestigate the 'i' command as although I have
been using sed for many years I had resigned myself to never being able
to get the 'i' command to work in one-liner scripts! After a few trials, I
now have the answer: break it up into a number of -e arguments, viz.,

sed -e '/^+/i\' -e 'Insert' file
or, sed -e '/^+/'i\\ -e 'Insert' file

As expected, identical one-liner syntax works for the 'a' command, also.

Note for MSDOS users: I found that the \ must not be inside any quotes,
i.e., you must use -e /^+/i\ OR "/^+/"i\ OR "/^+/i"\ but not "/^+/i\"

So, Daniel, you provided the incentive for me to master 'i' in one-liners
at last! In Unix, the usual work-around is to split the command over two
lines (but even this cop out is not available in the MSDOS OS).

-- 
John Savage                   (my news address is not valid for email)
 


Relevant Pages

  • Re: regex one liner
    ... We run into one of these "How do I do this in a one-liner?" ... Perl script and run it from the command-line. ... well just type out the command. ... The only drawback to oneliners (for things that can legitimately be ...
    (perl.beginners)
  • Re: Seeking help with Sed statement.
    ... I've been able to write the fix in awk, ... Your query prompted me to reinvestigate the 'i' command as although I have ... As expected, identical one-liner syntax works for the 'a' command, also. ... Note for MSDOS users: I found that the \ must not be inside any quotes, ...
    (comp.unix.shell)
  • Re: How do I speedup this loop?
    ... > I'm getting some output by running a command using os.popen. ... > after escaping some characters). ... script = ... I'm not advocating such a terse one-liner unless you are very comfortable ...
    (comp.lang.python)
  • Re: Bash alias ignored inside of function
    ... > Editing a multiline command is just as easy as a one-liner, ... > you get the readbility as well. ... > ^P) you get the entire multi-line command displayed, ... The alias seems to override the function. ...
    (comp.unix.shell)
  • Re: Removing lines from a plain text file
    ... Apparently there is no simple command. ... > and put prune in a cron job. ... > size by removing the excess from the beginning of the file. ... date from the past and use awk to search through the file for it and discard ...
    (comp.unix.sco.misc)