Seeking help with Sed statement.

Seeking help with Sed statement.

Post by l3gl3$$_m4r.. » Tue, 17 May 2005 04:30:51



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 m*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

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

 
 
 

Seeking help with Sed statement.

Post by Chris F.A. Johnso » Tue, 17 May 2005 04:46:00



> 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>

 
 
 

Seeking help with Sed statement.

Post by Pascal Bourguigno » Tue, 17 May 2005 04:53:00




> 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

Quote:}'

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.

 
 
 

Seeking help with Sed statement.

Post by l3gl3$$_m4r.. » Tue, 17 May 2005 08:16:29



Quote:>   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

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

 
 
 

Seeking help with Sed statement.

Post by Chris F.A. Johnso » Tue, 17 May 2005 08:28:10




>>   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

   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>

 
 
 

Seeking help with Sed statement.

Post by h41ry_p00.. » Tue, 17 May 2005 14:51:21





>>>   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
>   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

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

 
 
 

Seeking help with Sed statement.

Post by Alan Conno » Tue, 17 May 2005 15:43:09



<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

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


<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 week.

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 *ing 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 *ing 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 *y.

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://www.veryComputer.com/

 
 
 

Seeking help with Sed statement.

Post by Chris F.A. Johnso » Tue, 17 May 2005 16:57:56






>>>>   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

>>   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:

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

Quote:> 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
    adverti*t for awk.

--
    Chris F.A. Johnson                     <http://www.veryComputer.com/>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.veryComputer.com/~chris/books/ssr.html>

 
 
 

Seeking help with Sed statement.

Post by c4rp3t_munc.. » Wed, 18 May 2005 04:40:25



Quote:> Get your *ing 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 *y.
> 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 *.

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

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

 
 
 

Seeking help with Sed statement.

Post by M?ns Rullg?r » Wed, 18 May 2005 04:55:09




>> Get your *ing 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 *y.

>> 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 *.

> Switch to decaf, dude.

Alan Connor is a troll.  Ignore him.

--
M?ns Rullg?rd

 
 
 

Seeking help with Sed statement.

Post by Chris Croughto » Wed, 18 May 2005 20:03:45


On Mon, 16 May 2005 06:43:09 GMT, Alan Connor

Quote:> 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 week.

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

What do you know about 'Netiquette'?  Evidently not a lot, since you are
the one using 'inappropriate' language.

Quote:> 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.

Ooh, what a big boy, hiding behind an invalid address to flame someone
who has the temerity to have a couple of extra lines in a sig!  I know
which one is going in my killfile (and it isn't the flamed one)...

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

> Get your *ing bulletin board out of my face or get out of my
> newsreader.

Such a big boy using '*' words.  Most of us grew out of that sort
of thing years ago.

PLONK.

Chris C

 
 
 

Seeking help with Sed statement.

Post by John Savag » Thu, 19 May 2005 09:47:56



>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)

 
 
 

Seeking help with Sed statement.

Post by John Savag » Thu, 19 May 2005 10:48:14



>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?

I just reread your post since Chris pointed out you want only the
first instance changed, so my answer given earlier will need to become:

sed -e '1,/^+/{//i\' -e 'Insert' -e '}' file

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

 
 
 

Seeking help with Sed statement.

Post by $p1r17u4l_gyn3c0l0g.. » Fri, 20 May 2005 02:14:00



Quote:> 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.,

Much appreciated sir!

--
.............................................................................

"Forced counseling and "sensitivity training" are nothing more than
 buzzwords for political re-education"
                                         -Letter in NY Times, Feb 2, 2000

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

 
 
 

Seeking help with Sed statement.

Post by John Savag » Sun, 22 May 2005 09:42:05




>> - I want to insert the line "Insert" before the first line beginning with a
>> plus.  To my understanding of sed it would be:

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

Any reason for including that H instruction, Pascal?

Out of interest, any ideas on how you'd solve the problem were it changed
to the 5th (instead of the 1st) instance of a line matching /^+/ where the
inserted line was wanted?
--
John Savage                   (my news address is not valid for email)

 
 
 

1. Need some help with this awk/sed statement.

Hello all,
I have this script I have been going through to make sure it is suppose
to work.  I don't want any surprises when I run it.  I came across the
following and am missing what the author is trying to do.  Maybe you
guys/gals can open my eyes:

.......cut start....paste start.....

#######
# Modify default user directory location
#######

# need if statement here to detect if directory exist.
mkdir /`awk '{print $3}' /etc/hosts | sed '$!d'` /home

useradd -D -b /`awk '{print $3}' /etc/hosts | sed '$!d'` /home

........cut end.....paste end........

My first thought is " /etc/hosts does not necessarily have a $3 for
awk.  I could be just an IP address $1 and a host name $2. It looks
like the author is trying to make a /SomeHostName/home or something
directory.

Thanks to all.

2. kaddressbook - for those who couldn't help

3. Help with SED statement

4. msginfo_msgtql value

5. Help with if then statement and loop statement

6. HELP! Can't work SCSI ST-02 drvr with cp3200f. I'm DESPERATE!!!

7. sed sed sed

8. HOWTO for Linux Installation over NFS???

9. . 2 sed FAQs, sed exes, sed cetras; URLs

10. SED SED SED...

11. (sed 1q ; sed 2q) : no output from 2nd 'sed'

12. vmstat | sed WORKS vmstat | logger WORKS vmstat | sed | logger FAILS - NEED HELP

13. help help help about sed