windows - 清除特定文本文件行的内容而不删除回车
问题描述
我有一个 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
是 CR LF):
$ hexdump -C out
00000000 68 65 6c 6c 6f 20 79 6f 75 0d 0a 0d 0a 68 65 6c |hello you....hel|
00000010 6c 6f 20 77 6f 72 6c 64 0d 0a |lo world..|
解释:
$ 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
推荐阅读
- python - 使用 Pytorch 将列表作为特征并将列表作为目标/标签?
- python - 在python中循环函数并导出文件
- keycloak - 为什么 Keycloak admin-cli 客户端会收到 403 Forbidden 以列出组?
- javascript - 在邮递员上使用客户端和客户端密码获取 vimeo 访问令牌
- c# - 等待C#中的按键
- javascript - javascript obj.value 稍后在函数中消失
- android - 如何在android studio中将自定义相机捕获图像设置为网格视图
- spring-boot - 排除其他 Logging 依赖项,仅在 Spring Boot 应用程序中使用 Log4j2
- typescript - 为什么 TypeScript 会为具有数组类型联合的数组项推断此类型?
- python - 正确的输出,但对于结果中的最后一个元素,但我需要完整迭代的所有结果