qazwsx988
qazwsx988

Reputation: 55

Replace newline with string in sed on mac

In a file, I need to replace all newlines (not the escape sequence '\n', but the actual newline) with a string. All the questions I've found on SO have been the other way around; i.e. replacing a string with a literal newline. This is on a Mac.

I've tried the following

sed -i '' 's/\
/STOP/g' file.txt

But it gives me an "unterminated substitute pattern" error.

Upvotes: 4

Views: 3670

Answers (1)

anubhava
anubhava

Reputation: 785146

While it can be done using sed also but doing this with awk is much simpler:

awk -v ORS='STOP' '1' file

This changes output record separator to STOP instead of default \n.

Update: Here is a sed version to do same on OSX:

sed -i.bak -n -e 'H;${x;s/\n/STOP/g;p;}' file

Upvotes: 6

Related Questions