如何删除多个文件的尾随空格? [英] How to remove trailing whitespaces for multiple files?
问题描述
是否有任何工具/UNIX 单行可以删除多个文件的尾随空格就地.
Are there any tools / UNIX single liners which would remove trailing whitespaces for multiple files in-place.
例如一种可以与find结合使用的.
E.g. one that could be used in the conjunction with find.
推荐答案
你想要的
sed --in-place 's/[[:space:]]+$//' file
这将删除所有 POSIX 标准定义的空白字符,包括垂直制表符和换页符.此外,它只会在尾随空格实际存在时进行替换,这与使用零个或多个匹配器 (*
) 的其他答案不同.
That will delete all POSIX standard defined whitespace characters, including vertical tab and form feed. Also, it will only do a replacement if the trailing whitespace actually exists, unlike the other answers that use the zero or more matcher (*
).
--in-place
只是-i
的长形式.我更喜欢在脚本中使用长格式,因为它更能说明标志的实际作用.
--in-place
is simply the long form of -i
. I prefer to use the long form in scripts because it tends to be more illustrative of what the flag actually does.
它可以像这样轻松地与 find
集成:
It can be easily integrated with find
like so:
find . -type f -name '*.txt' -exec sed --in-place 's/[[:space:]]+$//' {} +
如果您使用的是 Mac
正如评论中所指出的,如果您没有安装 gnu 工具,上述方法将不起作用.如果是这种情况,您可以使用以下方法:
If you're on a Mac
As pointed out in the comments, the above doesn't work if you don't have gnu tools installed. If that's the case, you can use the following:
find . -iname '*.txt' -type f -exec sed -i '' 's/[[:space:]]{1,}$//' {} +
这篇关于如何删除多个文件的尾随空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!