Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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] 
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.