LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Helping using sed (https://www.linuxquestions.org/questions/linux-general-1/helping-using-sed-44957/)

LinuxQuest01 02-11-2003 12:49 PM

Helping using sed
 
Hi,

I have a file that contains
aaaaa
bbbbb
aaaaa
eeeee
junk
more junk
end
I want to look for junk. Once i locate 'junk' i want to insert preferably a line of text (e.g "Here's more junk!") after the line 'junk'.

Can someone show me how to do this?

Thanks in advanced.

Tinkster 02-11-2003 05:13 PM

sed -e "/junk/a\Here's more junk!" junk
 
Nice introduction to sed ;)

Btw, I copied the contents of your file into a file called junk
that I fed to sed ;)
If you need the output in a new file you'll probably do something
like
Code:

sed -e "/junk/a\Here's more junk!" junk > new_junk
or
Code:

sed -e "/junk/a\Here's more junk!" junk | tee new_junk
Cheers,
Tink

LinuxQuest01 02-12-2003 10:46 AM

Have you tried that command
 
Hi. Thanks for the response..

I tried the command you gave me and it's giving me this message:

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

is there something missing in your command,

sed -e "/junk/a\Here's more junk!" junk > new_junk

Also, sed will go through and put "Here's more junk!" after every instance of "junk" it finds. What if I only want to put that sentence after the 1st instance of the work "junk". I know you can used the line address for sed, but junk won't be on the same line every time.

Thx!

cyberskye 02-12-2003 12:55 PM

This what you are looking for? important parts in bold...

Skye

Quote:

[2addr]s/re/replacement/flags
Substitute the replacement string for the first instance of the
regular expression in the pattern space. Any character other
than backslash or newline can be used instead of a slash to de-
limit the RE and the replacement. Within the RE and the replace-
ment, the RE delimiter itself can be used as a literal character
if it is preceded by a backslash.

An ampersand (`&') appearing in the replacement is replaced by
the string matching the RE. The special meaning of `&' in this
context can be suppressed by preceding it by a backslash. The
string `\#', where `#' is a digit, is replaced by the text
matched by the corresponding backreference expression (see
re_format(7)).

A line can be split by substituting a newline character into it.
To specify a newline character in the replacement string, precede
it with a backslash.

The value of flags in the substitute function is zero or more of
the following:

0 ... 9
Make the substitution only for the N'th occurrence
of the regular expression in the pattern space.



Tinkster 02-12-2003 01:08 PM

Quote:

sed: -e expression #1, char 9: Extra characters after command
What does sed -V say?
The example works fine
Code:

GNU sed version 3.02.80
Did you copy & paste the example, or type it out?

As for the "only once" part ... I don't think that regexp
allow such a feature...

You'd have to go beyond a one-liner and feed
sed with a script that does the trick not with an
append, but rather with a substitution which (by
default, if you don't use the g-switch at the end)
will only replace the first occurence.
You'd be doing something like
[code]
s|junk|junk\nHere's more junk!|
[/ocde]

Cheers,
Tink

P.S.: You should really read the tutorial I posted ;)


All times are GMT -5. The time now is 05:19 AM.