Windows 清除特定文本文件行的内容,而不删除回车

Windows 清除特定文本文件行的内容,而不删除回车,windows,sed,replace,carriage-return,Windows,Sed,Replace,Carriage Return,我有一个DOS文本文件,我想从中清除以磅符号开头的行的所有内容。我想在每一行中保留回车符(CR),这不适用于下面的代码 正如我对“*”的理解,除换行符(LF)外的任何字符都被视为。还有CR,这就是为什么我的想法是用CR替换行内容 这就是我所拥有的: sed.exe -e "s/^#.*/ \r/g" %1 >> result.txt 我希望发生的是,例如文本文件: hello you CRLF #hello me CRLF hello world CRLF 更改为 hello y

我有一个DOS文本文件,我想从中清除以磅符号开头的行的所有内容。我想在每一行中保留回车符(CR),这不适用于下面的代码

正如我对“*”的理解,除换行符(LF)外的任何字符都被视为。还有CR,这就是为什么我的想法是用CR替换行内容

这就是我所拥有的:

sed.exe -e "s/^#.*/ \r/g" %1 >> result.txt
我希望发生的是,例如文本文件:

hello you CRLF
#hello me CRLF
hello world CRLF
更改为

hello you CRLF
 CRLF
hello world CRLF
但结果实际上是

hello you CRLF
 rLF
hello world CRLF
如何使CR保持在线?

您能处理awk吗

测试源文件行结尾:

$ file file
file: ASCII text, with CRLF line terminators
awk:

$ awk 'BEGIN{RS=ORS="\r\n"}{sub(/^\#.*/,"")}1' file > out
查看结果(
0d 0a
is CR LF):

解释:

$ awk '
BEGIN {               # set the record separators to CR LF
    RS=ORS="\r\n"     # both, input and output
}
{
    sub(/^\#.*/,"")   # replace # starting records with ""
}1' file > out        # output and redirect it to a file
$ awk '
BEGIN {               # set the record separators to CR LF
    RS=ORS="\r\n"     # both, input and output
}
{
    sub(/^\#.*/,"")   # replace # starting records with ""
}1' file > out        # output and redirect it to a file