LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 06-11-2009, 08:15 PM   #1
saurabhchokshi
Member
 
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?

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

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

Rep: Reputation: 46
Moin,

do you mean this?

Code:
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
Jan
 
  


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 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 04:01 PM
how to use the sed w option to redirect pattern match to file nickleus Linux - General 11 04-18-2006 09:34 AM
SED - replace / insert furquan Programming 5 03-01-2006 07:58 PM
vim: search pattern / replace with file naflan Programming 5 01-07-2006 04:16 PM
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 08:40 AM


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

Main Menu
Advertisement
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