|
Home > Archive > Unix Programming > May 2005 > Seeking help with Sed statement.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Seeking help with Sed statement.
|
|
| l3gl3$$_m4r1n3@salmahayeksknockers.edu 2005-05-15, 5:49 pm |
| 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@m3m3t1ccand1ru.com http://www.memeticcandiru.com
| |
| Chris F.A. Johnson 2005-05-15, 5:49 pm |
| On Sun, 15 May 2005 at 19:30 GMT, l3gl3$$_m4r1n3@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>
| |
| Pascal Bourguignon 2005-05-15, 5:49 pm |
| l3gl3$$_m4r1n3@salmahayeksknockers.edu writes:
l3gl3$$_m4r1n3@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.
| |
| l3gl3$$_m4r1n3@salmahayeksknockers.edu 2005-05-15, 8:47 pm |
| In comp.unix.shell Chris F.A. Johnson <cfajohnson@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@m3m3t1ccand1ru.com http://www.memeticcandiru.com
| |
| Chris F.A. Johnson 2005-05-15, 8:47 pm |
| On Sun, 15 May 2005 at 23:16 GMT, l3gl3$$_m4r1n3@salmahayeksknockers.edu wrote:
> In comp.unix.shell Chris F.A. Johnson <cfajohnson@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>
| |
| h41ry_p0073r@salmahayeksknockers.edu 2005-05-16, 2:56 am |
| In comp.unix.shell Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
> On Sun, 15 May 2005 at 23:16 GMT, l3gl3$$_m4r1n3@salmahayeksknockers.edu wrote:
[vbcol=seagreen]
> 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@m3m3t1ccand1ru.com http://www.memeticcandiru.com
| |
| Alan Connor 2005-05-16, 2:56 am |
| On comp.unix.shell, in <txWhe.64133$HR1.28163@clgrps12>, "h41ry_p0073r@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@OOOOOOOOOOOOOO.OOO OOOO://OOO.OOOOOOOOOOOOOO.OOO
<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 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 XXX 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
| |
| Chris F.A. Johnson 2005-05-16, 2:56 am |
| On Mon, 16 May 2005 at 05:51 GMT, h41ry_p0073r@salmahayeksknockers.edu wrote:
> In comp.unix.shell Chris F.A. Johnson <cfajohnson@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>
| |
| c4rp3t_munch3r@salmahayeksknockers.edu 2005-05-16, 5:52 pm |
| In comp.unix.shell Alan Connor <zzzzzz@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 XXX 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@m3m3t1ccand1ru.com http://www.memeticcandiru.com
| |
| Måns Rullgård 2005-05-16, 5:52 pm |
| c4rp3t_munch3r@salmahayeksknockers.edu writes:
> In comp.unix.shell Alan Connor <zzzzzz@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.
--
Måns Rullgård
mru@inprovide.com
| |
| Chris Croughton 2005-05-17, 7:48 am |
| On Mon, 16 May 2005 06:43:09 GMT, Alan Connor
<zzzzzz@xxx.yyy> wrote:
> 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.
> 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)...
> Fix your sig or I'll killfile you, JERK.
>
> Get your XXXXing bulletin board out of my face or get out of my
> newsreader.
Such a big boy using 'naughty' words. Most of us grew out of that sort
of thing years ago.
PLONK.
Chris C
| |
| Sean Burke 2005-05-17, 5:57 pm |
|
Chris Croughton <chris@keristor.net> writes:
> Such a big boy using 'naughty' words. Most of us grew out of that sort
> of thing years ago.
>
> PLONK.
Let us know when you grow out of needing to get the last word in
before plonking an obvious troll. :-/
-SEan
| |
| John Savage 2005-05-17, 8:48 pm |
| 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)
| |
| John Savage 2005-05-17, 8:48 pm |
| 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?
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)
| |
| $p1r17u4l_gyn3c0l0g1$7@salmahayeksknockers.edu 2005-05-18, 6:03 pm |
| In comp.unix.shell John Savage <rookswood@suburbian.com.au> wrote:
> 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
..............................................................................
dswan@m3m3t1ccand1ru.com http://www.memeticcandiru.com
| |
| John Savage 2005-05-20, 8:48 pm |
| Pascal Bourguignon <pjb@informatimago.com> writes:
>l3gl3$$_m4r1n3@salmahayeksknockers.edu writes:
>
>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)
| |
| Pascal Bourguignon 2005-05-20, 8:48 pm |
| John Savage <rookswood@suburbian.com.au> writes:
> Pascal Bourguignon <pjb@informatimago.com> writes:
>
> Any reason for including that H instruction, Pascal?
Some cruft remaining from the building of the expression...
> 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?
sed -e '/^+/n' -e '/^+/n' -e '/^+/n' -e '/^+/n' -e '/^+/{
i\
Insert
:copy
n
b copy
}'</tmp/file
--
__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.
| |
| John Savage 2005-05-25, 8:53 pm |
| Pascal Bourguignon <pjb@informatimago.com> writes:
>John Savage <rookswood@suburbian.com.au> writes:
>
>sed -e '/^+/n' -e '/^+/n' -e '/^+/n' -e '/^+/n' -e '/^+/{
>i\
>Insert
>:copy
>n
>b copy
>}'</tmp/file
>
I think that will only work in special cases, such as when the matching
lines are all consecutive. But I get your idea, you'd have 4 loops before
the insert instruction. It would be a bit tedious for say, the 25th instance,
but using the -f flag would be repetitious therefore easy to compose in
an editor.
--
John Savage (my news address is not valid for email)
|
|
|
|
|