4

It is a bit confusing as to what character(s) are used in Windows/DOS version of GNU sed to terminate a line. In particular, what is the newline (\r\n vs \n) char used to join 2 lines after an N command?

What I want is to create a script that combine C++ source lines that is using the line continuation character \ syntax.

e.g.

int \
a=\
10;

should be combined into this by my script:

int a=10;

Obviously I should use something like the "N" command and then the 's' command to substitute anything in the pattern space that looks like \ followed by a newline char with an empty string. But in Windows is this newline char \r\n or \n after an "N" command?

And should I use \\\r\n or \\\n to search for the lines with the line continuation pattern?

JavaMan
  • 4,954
  • 4
  • 41
  • 69

3 Answers3

1

I'm not exactly sure about the Windows/DOS version of sed, but if it is like the version in this question, it will magically turn \r\n into \n for processing. Like in the linked question, you will probably need an extra s/$/\r/ to put the \r back in. I tested

sed -e :a -e '/\\$/N; s/\\\n//; ta; s/$/\r/'

in Cygwin, and it seems to work.

Community
  • 1
  • 1
seeker
  • 1,136
  • 1
  • 13
  • 16
0

And should I use \\r\n or \\n to search for the lines with the line continuation pattern?

Why not use both? i.e. in sed

s/\r\?\n//g

which will match both \r\n or \n. ? might not need to be escaped if you're using ERE.

doubleDown
  • 8,048
  • 1
  • 32
  • 48
0

You don't have to worry about \r\n vs \n when using GNU sed on Windows. The program opens the file in "text" mode which treats \r\n as if it were a \n character on Unix systems.

The '-b' or '--binary' option can be used on Windows machines when you want to disable this mode, and treat the \r as a non-end-of-line character, such as searching for a '\r' in the "middle" of a line (i.e. terminated by '\n')

Bruce Barnett
  • 904
  • 5
  • 11