bash - replace \n with blank but keep \r\n using sed/tr -


my input looks like:

line 1\n  more line 1\r\n line 2\n  more line 2\r\n 

i want produce

line 1 more line 1\r\n line 2 more line 2\r\n 

using sed or tr. have tried following , not work:

sed -e 's/([^\r])\n/ /g' in_file > out_file 

the out_file still looks in_file. have option of writing python script.

try:

sed '/[^\r]$/{n; s/\n/ /}' in_file 

how works:

  • /[^\r]$/

    this selects lines not have carriage-return before newline character. commands follow in curly braces executed lines match regex.

  • n

    for selected lines, appends newline , reads in next line , appends it.

  • s/\n/ /

    this replaces unwanted newline space.

discussion

when sed reads in lines 1 @ time not read in newline character follows line. thus, in following, \n never match:

sed -e 's/([^\r])\n/ /g' 

we can match [^\r]$ $ signals end-of-the-line still not include \n.

this why n command used above read in next line, appending pattern space after appending newline. makes newline visible , can remove it.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -