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. |
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 Code:
sed -e "/junk/a\Here's more junk!" junk | tee new_junk Tink |
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! |
This what you are looking for? important parts in bold...
Skye Quote:
|
Quote:
The example works fine Code:
GNU sed version 3.02.80 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. |