Unix/Linux
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   Web Development Archives FAQs Unix/Linux

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Web Development Archives Sponsor:
  #1  
Old May 9th, 2005, 11:51 AM
Rob Eger
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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

Reply With Quote
  #2  
Old May 9th, 2005, 11:51 AM
Ed Morton
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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.


Reply With Quote
  #3  
Old May 9th, 2005, 11:51 AM
Damian Ibbotson
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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

Reply With Quote
  #4  
Old May 9th, 2005, 11:51 AM
Ed Morton
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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
>



Reply With Quote
  #5  
Old May 9th, 2005, 11:51 AM
rakesh sharma
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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)

Reply With Quote
  #6  
Old December 28th, 2006, 08:52 PM
zubair zubair is offline
Registered User
Dev Archives Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 1 zubair User rank is Just a Lowly Private (1 - 20 Reputation Level)  
Time spent in forums: 8 m 48 sec
Reputation Power: 0
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

Reply With Quote
Reply

Viewing: Web Development Archives FAQs Unix/Linux > inserting lines before and after lines with matching pattern using sed?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
Stay green...Green IT