如何删除多个文件的尾部空格? [英] How to remove trailing whitespaces for multiple files?
问题描述
您想要
sed --in-place's / [[:space:]] \ + $ //'file
这将删除所有 POSIX标准定义的空白字符,包括垂直制表符和换页符。另外,如果尾部空白实际存在,它将只进行替换,而不像其他使用零或更多匹配的答案( *
)。 b
- 就地
只是 -i
的长形式。我更喜欢在脚本中使用长格式,因为它往往更能说明标记实际上做了什么。
它可以很容易地与找到
就像这样:
find。 -type f -name'* .txt'-exec sed --in-place's / [[:space:]] \ + $ //'{} \ +
如果您在Mac上
正如评论中指出的那样,如果您没有安装gnu工具,则不起作用。如果是这样的话,可以使用下面的代码:
find。 -iname'* .txt'-type f -exec sed -i''s / [[:space:]] \ {1,\} $ //'{} \ +
Are there any tools / UNIX single liners which would remove trailing whitespaces for multiple files in-place.
E.g. one that could be used in the conjunction with find.
You want
sed --in-place 's/[[:space:]]\+$//' file
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
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.
It can be easily integrated with find
like so:
find . -type f -name '*.txt' -exec sed --in-place 's/[[:space:]]\+$//' {} \+
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屋!