Linux Bash:查找行尾尾随空格的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11210126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:04:16  来源:igfitidea点击:

Bash: find files with trailing spaces at the end of the lines

linuxbashtrailing

提问by ziiweb

I'm looking for a bash command to find files with trailing spaces at the end of each line. I'm not interested in removing the spaces, but just in finding the files.

我正在寻找一个 bash 命令来查找每行末尾带有尾随空格的文件。我对删除空格不感兴趣,而只是对查找文件感兴趣。

回答by matcheek

Find files with one or more trailing space characters:

查找带有一个或多个尾随空格字符的文件:

find . -name "*" | xargs egrep ".* +$"

回答by pizza

Find files that has trailing spaces.

查找尾随空格的文件。

find . -type f -exec egrep -l " +$" {} \;

回答by Acumenus

If the goal is to list files which have trailing whitespaces in one or more lines:

如果目标是在一行或多行中列出尾随空格的文件:

grep -r '[[:blank:]]$' .

To not print the lines, and print just the file names only, also specify the -loption. That's las in the word list, not the number 1.

要不打印行,而只打印文件名,还要指定该-l选项。这是l因为在这个词list,而不是数量1

回答by tripleee

There is an option to list the files which do not contain a match anywhere in them; use that and a regex for a character other than a space just before end of line.

有一个选项可以列出其中任何地方都不包含匹配项的文件;将它和正则表达式用于行尾之前的空格以外的字符。

grep -L '[^ ]$' *

To recurse directories, add -r. To search for other whitespace characters as well, use a character class $'[^ \t]$'or the POSIX '[^[:blank:]]$'for the regex.

要递归目录,请添加-r. 要搜索其他空白字符,请对正则表达式使用字符类$'[^ \t]$'或 POSIX '[^[:blank:]]$'

回答by twalberg

If the question is literally to find files that have a blank at the end of every single line, then this should work:

如果问题实际上是要查找每一行末尾都有空格的文件,那么这应该有效:

grep -rL '[^[:blank:]]$' .

The -Ltells grepto report every file that does not match the pattern, and the pattern is looking for lines that do not have a blank immediately preceding the newline.

-L告诉grep记者了解每个不匹配的模式文件,并将该图案寻找没有空白的方式来隐藏线。

回答by friederbluemle

Using ack(or ag):

使用ack(或ag):

ack -l ' \n'

Note: Like some of the other answers, this will list files that contain one or morelines with trailing spaces.

注意:与其他一些答案一样,这将列出包含一行或多行尾随空格的文件。

回答by class

If the goal is to list fileswith trailing whitespacein the current path:

如果目标是到列表文件尾随空格当前路径

grep -rli '[[:blank:]]$' .

回答by user10914363

Try this :

尝试这个 :

find . -type f -name "* "