|
|
|
|
|||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
inserting lines before and after lines with matching pattern using sed?
I need to go through a file and wherever I find lines that contain a
certain string I need to insert a line above and below that line. Lines that don't contain the string can go straight into the new file. I've been trying various sed commands to no avail. Can this be done with sed, or am I wasting my time and need to go at it a different way? If it can be done with sed, how? Thanks, Rob Eger Tech-X Corporation Boulder, C |
|
#2
|
|||
|
|||
|
inserting lines before and after lines with matching patternusing sed?
7/22/2003 10:18 AM, Rob Eger wrote: I need to go through a file and wherever I find lines that contain a certain string I need to insert a line above and below that line. Lines that don't contain the string can go straight into the new file. > I've been trying various sed commands to no avail. Can this be done with sed, or am I wasting my time and need to go at it a different way? If it can be done with sed, how? This: sed -e '/a certain string/{ s/^/X\ / s/$/\ Y/ } ' Will put an "X" on the line before every occurrence of "a certain string" and a "Y" on the succeeding line. Ed. |
|
#3
|
|||
|
|||
|
inserting lines before and after lines with matching pattern using sed?
awk '/PATTERN/ {print "header"; print; print "trailer"; next} {print}' yourFile -- Posted via http://dbforums.com |
|
#4
|
|||
|
|||
|
inserting lines before and after lines with matching patternusing sed?
7/22/2003 11:21 AM, Charles Demas wrote: In article <9cc83d20.0307220718.56ad7182@posting.google.com>, Rob Eger <reger@txcorp.comwrote: > <snip> It can probably be done with sed, but doing it with awk is easier, IM > Untested code: > awk '/certain string/ {print "Before stuff"; print; print "After stuff"; next} {print}' infile outfile > or > awk '/certain string/ {print "Before stuff"} {print} /certain string/ {print "After stuff"}' infile outfileprint > Both fine, though I'd probably do it this way in awk: awk '/certain string/ {b="Before stuff\n";a="\nAfter stuff"} {print b $0 a;b=a=""}' or: awk '{o=$0} /certain string/ {o="Before stuff\n"o"\nAfter stuff"} {print o}' or (in a ?awk where you can set $0): gawk '/certain string/ {$0="Before stuff\n"$0"\nAfter stuff"} {print}' Regards, Ed. > Chuck Demas > |
|
#5
|
|||
|
|||
|
inserting lines before and after lines with matching pattern using sed?
reger@txcorp.com (Rob Eger) wrote in message news:<9cc83d20.0307220718.56ad7182@posting.google.com>
I need to go through a file and wherever I find lines that contain a certain string I need to insert a line above and below that line. Lines that don't contain the string can go straight into the new file. > I've been trying various sed commands to no avail. Can this be done with sed, or am I wasting my time and need to go at it a different way? If it can be done with sed, how? > Thanks, Rob Eger > Tech-X Corporation Boulder, C sed -e ' /string/i\ BEFRE /string/a\ AFTER ' inputfile ( I am assuming you are using bourne shell. C-shell quoting would be different) |
|
#6
|
|||
|
|||
|
I am trying to insert a line when a pattern matching is found
I have $marker=">>>>" $tag="<table cellspacing=0 cellpadding=0 border=0>" I am doing this inside my bash script cat $inputfile | awk '/$marker1/ {print $tag1} {print}' >> $outputfile Nothing is happening, the outputfile is still same as input file. Where I am doing wrong. Any help will be appreciated. thanks |
![]() |
| Viewing: Web Development Archives > FAQs > Unix/Linux > inserting lines before and after lines with matching pattern using sed? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|