inserting lines before and after lines with matching pattern using sed?

inserting lines before and after lines with matching pattern using sed?

Post by Rob Eg » Thu, 24 Jul 2003 00:18:38



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, CO

 
 
 

inserting lines before and after lines with matching pattern using sed?

Post by Charles Dem » Thu, 24 Jul 2003 01:21:39




>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?

It can probably be done with sed, but doing it with awk is easier, IMO.

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

Chuck Demas

--
  Eat Healthy        |   _ _   | Nothing would be done at all,

  Die Anyway         |    v    | That no one could find fault with it.


 
 
 

inserting lines before and after lines with matching pattern using sed?

Post by Ed Morto » Thu, 24 Jul 2003 01:14:26



Quote:> 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/

Quote:}

'

Will put an "X" on the line before every occurrence of "a certain string" and a
"Y" on the succeeding line.

        Ed.

 
 
 

inserting lines before and after lines with matching pattern using sed?

Post by Damian Ibbotso » Thu, 24 Jul 2003 01:40:12


awk '/PATTERN/ {print "header"; print; print "trailer"; next}
{print}' yourFile

--
Posted via http://dbforums.com

 
 
 

inserting lines before and after lines with matching pattern using sed?

Post by Ed Morto » Thu, 24 Jul 2003 04:37:23





<snip>
> It can probably be done with sed, but doing it with awk is easier, IMO.

> 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.

- Show quoted text -

Quote:

> Chuck Demas

 
 
 

inserting lines before and after lines with matching pattern using sed?

Post by rakesh shar » Thu, 24 Jul 2003 05:48:16



> 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, CO

sed -e '
        /string/i\
BEFORE
        /string/a\
AFTER
' inputfile

( I am assuming you are using bourne shell. C-shell quoting would be different)

 
 
 

inserting lines before and after lines with matching pattern using sed?

Post by rakesh shar » Thu, 24 Jul 2003 11:20:20



> 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, CO

sed -e '/regex/!b
        i\
BEFORE
        a\
AFTER' inputfile

or,

sed -e '/regex/!b
        s/^/BEFORE\
/;s/$/\
AFTER/' inputfile

(note: there should be nothing after the trailing \ above, as they are
there to escape the newline)

using nawk:

nawk '/regex/{$0="BEFORE\n"$0"\nAFTER"}1' inputfile

or with perl:

perl -ple '$_="BEFORE\n$_\nAFTER"if/regex/' inputfile

 
 
 

1. Matching a pattern in a file and inserting variable string above the line matched?

Hello all,  here is a snipet of code for a problem i'm having.  I've
tried to use sed for this, but i cannot get the variable substitution
worked out.

print "Please enter each username(s) you wish to add: "      
read NAMES
for USER in $NAMES ; do
  if egrep "^$USER" /etc/passwd > /dev/null ; then
       ### insert sed, awk, or perl code here
  fi
done

### The layout of the file is:
root ADMIN=ALL JBP=ALL
* ADMIN=JBP JBP=ENDUSER+BU+ARC

For each $USER, I need to add the following string ABOVE
the "* ADMIN=JBP" line in the file:  $USER ADMIN=ALL JBP=ALL

Thanks in advance for any help.

jim

2. Linux (KDE) Hardware monitoring

3. inserting a blank line after every line - using sed

4. Unix Admin Resources (JOBS, RESUMES, LINKS)!

5. How to replace a whole line (matching a pattern) in a text filke with SED ?

6. Printing in Solaris

7. Pattern Matching Multiple Lines with SED and AWK

8. Hilfe !!! Linux PPP zu T-Online bekomme ich nicht hin

9. Sed: merging lines recursively depending on line pattern

10. Delete a whole line with sed, then insert diff text on same line...

11. Matching Line After Pattern (Pattern Occurs Multiple Times)

12. How do I insert line numbers before each line using ed?