0

I am trying to replace a string that contains CR and LF in the text of a file.

So the input file

abcd
efgh
ijkl

Becomes

abcd
zxgh
ijkl

literally I wish the string "cd + CR + LF + ef" to be replaced with "cd + CR + LF + zx". I have tried

sed -e 's/cd\red/cd\rzx/g' file
sed -e 's/cd\r\ned/cd\r\nzx/g' file

without success

xavi
  • 109
  • 6
  • Does this answer your question? [Remove carriage return in Unix](https://stackoverflow.com/questions/800030/remove-carriage-return-in-unix) – Benjamin W. May 20 '20 at 18:07

2 Answers2

1

Switch to Perl.

perl -0777 -pe 's/cd\r\nef/cd\r\nzx/' file

Or with GNU sed:

sed '/cd\r$/{N;s/cd\r\nef/cd\r\nzx/}' file

Output:

abcd
zxgh
ijkl
Cyrus
  • 84,225
  • 14
  • 89
  • 153
0

With sed, you can do it as follows:

sed '/cd\r$/N;s/cd\r\nef/cd\r\nzx/' file
Pierre François
  • 5,850
  • 1
  • 17
  • 38