1

I'm using sed to edit a text file. The command:

sed -n -e '/pattern1/,/pattern2/p' test01.txt

show the lines between pattern1 and pattern2. But how can I insert a new line after this pattern2? my sed version don't admit "i\" for line-command edition. I'm running a FreeBSD port.

1 Answer 1

2

If you just want to insert a new line after pattern2 then this would work -

sed '/pattern2/s/$/\n/' file

[jaypal:~/Temp] cat file
some text
some text
some text pattern1
some text p
some text p 
some text p
some text pattern2
some text
some text

[jaypal:~/Temp] sed '/pattern2/s/$/\n/' file
some text
some text
some text pattern1
some text p
some text p 
some text p
some text pattern2

some text
some text

i\ is for inserting. It would insert before an address. If you need a new line you would use \a which is append.

[jaypal:~/Temp] sed '/pattern2/ a\
' file
some text
some text
some text pattern1
some text p
some text p 
some text p
some text pattern2

some text
some text

If you want to add a new line after your /pattern2/ and view lines between them, then may be you can do something like this -

[jaypal:~/Temp] sed -e '/pattern2/G' -ne '/pattern1/,/pattern2/p' file
some text pattern1
some text p
some text p 
some text p
some text pattern2

[jaypal:~/Temp] 

Similar solution in awk -

[jaypal:~/Temp] cat file
some text
some text
some text pattern1
some text p
some text p 
some text p
some text pattern2
some text
some text

[jaypal:~/Temp] awk '/pattern1/,/pattern2/ END{print ""}' file
some text pattern1
some text p
some text p 
some text p
some text pattern2

[jaypal:~/Temp] 
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .