Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-11-2009, 07:15 PM   #1
Registered: May 2007
Posts: 35

Rep: Reputation: 15
Sed to read the file and replace and insert the pattern

Hi ,

I want to read from the file and check for the pattern, if the line has some word like <string>: then string should be copied into buffer.

Afterwards, I want to insert the same <string> with some word in the next line of the file.

Could you please help me out to use sed command to perform the above mentioned operations?

Old 06-12-2009, 02:03 AM   #2
Stéphane Ascoët
Registered: Feb 2004
Location: Orleans, 120 km south of Paris
Distribution: Mandrake, Freeduc (the one I'm making), Slackware, MacOS X
Posts: 168

Rep: Reputation: 40
I think you'd better use awk
Old 06-12-2009, 01:15 PM   #3
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46

do you mean this?

jan@jack:~/tmp> echo 'some text without the pattern
> and then the text with <string>: occurence
> and then the last line' | \
> sed -r '/<string>:/{p;s/.*<(string)>:.*/new word and \1/;}'
some text without the pattern
and then the text with <string>: occurence
new word and string
and then the last line


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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
replace a pattern with a line using sed/awk lokeshn05 Linux - Newbie 3 05-06-2009 03:01 PM
how to use the sed w option to redirect pattern match to file nickleus Linux - General 11 04-18-2006 08:34 AM
SED - replace / insert furquan Programming 5 03-01-2006 06:58 PM
vim: search pattern / replace with file naflan Programming 5 01-07-2006 03:16 PM
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 07:40 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:41 PM.

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