GNU sed on Windows and the Line Termination Character

It is a little confusing which character (s) are used in the Windows / DOS version of GNU sed to end a line. In particular, what is the new line (\ r \ n vs \ n) char used to concatenate two lines after the N command?

I want to create a script that concatenate C ++ source strings that use the \ syntax line continuation character.

eg.

int \ a=\ 10; 

should be combined into this using a script:

 int a=10; 

Obviously, I should use something like the ā€œNā€ command and then the ā€œsā€ command to replace anything in the pattern space that looks like \ followed by a new char line with an empty line. But on Windows, is this a new line char \ r \ n or \ n after the "N" command?

And do I need to use \\\ r \ n or \\\ n to search for lines with a line continuation pattern?

+4
source share
3 answers

I'm not quite sure about the Windows / DOS version of sed, but if it is similar to the version in this question , it will magically turn \r\n into \n for processing. As in the related question, you probably need extra s/$/\r/ to return \r . I tested

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

at Cygwin, and it seems to work.

0
source

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

Why not use both? i.e. in sed

 s/\r\?\n//g 

which will match both \r\n and \n . ? shielding may not be necessary if you use ERE.

0
source

You do not need to worry about \ r \ n vs \ n when using GNU sed on Windows. The program opens the file in text mode, which processes \ r \ n as if it were the \ n character on Unix systems.

The option '-b' or '-binary' can be used on Windows computers if you want to disable this mode and treat \ r as a linear character , for example, searching for "\ r" in the "middle" of the string (i.e. ending "\ n ")

0
source

Source: https://habr.com/ru/post/1379258/


All Articles