Search This Blog

2014-06-19

Find and Replace Newline Followed by Another Pattern

I have file that looks like:
G1-15
y
G1-16
G1-17
y

I want to basically find and replace "\n y" with " y" to import to excel easily using sed, tr, awk, perl or something.

Found out the way to accomplish this task is different in FreeBSD from Linux

Linux Implementation

sed ':a;N;$!ba;s/\n y/ y/g' tmp
G1-15 y
G1-16
G1-17 y

http://backreference.org/2009/12/23/how-to-match-newlines-in-sed/
http://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n

FreeBSD Implementation

sed -e ':a' -e 'N' -e '$!ba' -e 's/\n y/ y/g' tmp
G1-15 y
G1-16
G1-17 y
http://stackoverflow.com/questions/9983646/sed-substitute-recursively
under the 'green checkmark' solution: Due to BSD Sed's quirkiness the embedded newlines are required. If you're using GNU Sed you may be able to get away with

No comments:

Post a Comment