Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I run sed to do some substitution on windows and I noticed that it automatically converts line endings to Unix ( ). Is there an option to tell sed to use Windows line endings ( ) or even better to preserve the line endings from the file?

Note: I use sed from unxutils: http://unxutils.sourceforge.net/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
518 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use the -b option for sed to have it treat the file as binary. This will fix the problem with cygwin's sed on Windows.

Example: sed -b 's/foo/bar/'

If you wish to match the end of the line, remember to match, capture and copy the optional carriage return.

Example: sed -b 's/foo( ?)$/bar1/'

From the sed man page:

-b ?????--binary

This option is available on every platform, but is only effective where the operating system makes a distinction between text files and binary files. When such a distinction is made—as is the case for MS-DOS, Windows, Cygwin—text files are composed of lines separated by a carriage return and a line feed character, and sed does not see the ending CR. When this option is specified, sed will open input files in binary mode, thus not requesting this special processing and considering lines to end at a line feed.`


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...