I'm using sed on windows to delete some superfluous lines in a unix style format (\n line endings). Unfortunately, sed开发者_StackOverflow中文版 replaces these line endings even in lines it does not change to \r\n. How can I stop sed from doing that?
My sed is a simple sed-for-windows-standalone-exe:
C:\dev>sed --version
super-sed version 3.59
based on GNU sed version 3.02.80
GNU sed ( http://gnuwin32.sourceforge.net/packages/sed.htm ) has the -b option for "binary mode", i. e. not replacing \n with \r\n.
If you use the sed that comes with cygwin then it usually uses the binary mode even without the -b option. Namely, cygwin commands use the input file path to decide whether they should run in text or binary mode, i. e. outputting \r\n or \n: http://www.cygwin.com/cygwin-ug-net/using-textbinary.html. As the document says, binary mode is the default for MS-DOS pathnames, and in my experience the filesystems mounted by default are also mounted in binary mode.
If you add the parameter -b you treat the file as a binary file and it won't change your line endings. The manual states:
-b, --binary
open files in binary mode (CR+LFs are not processed specially)
I do not know how you can do that with sed on Windows but have you tried:
unix2dos you_file
sed ...
dos2unix you_file
Sorry, but sed doesn't seem to work as you would like, i.e., sed != awk (which is configurable).
I downloaded the ssed executable and the help output did not mention any option for this, as I'm sure you know.
You could try to modify the source code, or contacting the authors.
Reading thru the NEWS file in the source code, I found
* The s/// command now understands the following escape (in both halves):
\a an "alert" (BEL)
\f a form-feed
\n a newline
\r a carriage-return
\t a horizontal tab
\v a vertical tab
\oNNN a character with the octal value NNN
\dNNN a character with the decimal value NNN
\xNN a character with the hexadecimal value NN
Have you tried `s/\r//' as the last command in your script?
I did a quick scan in most of the text files, but didn't find anything that leads me to believe there a cmd-line option that will give what you need.
As you don't want to use unix2dos, as an act of sheerest optimism, I offer the option of using tr to cleanout those pesky '\r's
sed -i -f yourSedScript yourFile
mv yourFile yourFile.wrk
tr -d '\015' yourFile.wrk > yourFile
Finally, as it seems that if you are editing Unix files on a windows box, you must be transfering files via 'ftp' or similar to get your Unix to Windows, why not rely on the ftp options to convert line endings?
I hope this helps.
精彩评论