LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-11-2003, 12:49 PM   #1
LinuxQuest01
Member
 
Registered: Jun 2002
Distribution: RH 7.3
Posts: 30

Rep: Reputation: 15
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.
 
Old 02-11-2003, 05:13 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,962
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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

Last edited by Tinkster; 02-11-2003 at 05:15 PM.
 
Old 02-12-2003, 10:46 AM   #3
LinuxQuest01
Member
 
Registered: Jun 2002
Distribution: RH 7.3
Posts: 30

Original Poster
Rep: Reputation: 15
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!
 
Old 02-12-2003, 12:55 PM   #4
cyberskye
Member
 
Registered: Feb 2003
Location: The City by the Bay
Posts: 116

Rep: Reputation: 15
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.


Last edited by cyberskye; 02-12-2003 at 12:57 PM.
 
Old 02-12-2003, 01:08 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,962
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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 ;)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Helping My Debian st3alth Linux - Newbie 4 11-09-2005 01:45 PM
helping with patitions tecknics Linux - Newbie 3 06-17-2004 05:03 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
looking forward to asking/helping shizin Slackware 1 03-08-2003 09:06 AM
Helping us Help you to help us chingasman Linux - General 1 12-31-2002 04:22 PM


All times are GMT -5. The time now is 12:39 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration