LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Sed to read the file and replace and insert the pattern (https://www.linuxquestions.org/questions/programming-9/sed-to-read-the-file-and-replace-and-insert-the-pattern-732333/)

saurabhchokshi 06-11-2009 07:15 PM

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

Stéphane Ascoët 06-12-2009 02:03 AM

I think you'd better use awk

jan61 06-12-2009 01:15 PM

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


All times are GMT -5. The time now is 07:50 PM.