Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我知道如何从文件中删除所有尾随空格,例如:
sed -i 's/ *$//' file
有没有办法做到这一点,但不是只包含空格的行?
本着以下精神的东西:
sed -i 's/[a-zA-Z0-9;}{] *$/[a-zA-Z0-9;}{]/' file ^ keep the original characters
最好但不是必须使用 sed。任何支持 linux 的解决方案都可以。
谢谢
只要确保一些其他字符出现在之前:
sed -r 's/([^\s])\s+$/\1/' file
这将检查是否出现非空格字符 ( \s) 后跟任意数量的空格。如果是这样,只需将这个非空格字符打印回来,以便删除尾随空格。
\s
用于cat -vet查看标记:
cat -vet
$ cat -vet a hello $ $ bye $ $ sed -r 's/([^\s])\s+$/\1/' a | cat -vet - hello$ $ bye$