如何删除整个项目中所有源代码末尾的空格?从根目录开始,移除所有文件夹中所有文件中的末尾空格。
此外,我希望能够直接修改文件,而不仅仅是将所有内容打印到标准输出流中。
如何删除整个项目中所有源代码末尾的空格?从根目录开始,移除所有文件夹中所有文件中的末尾空格。
此外,我希望能够直接修改文件,而不仅仅是将所有内容打印到标准输出流中。
这里是针对OS X 10.6及以上版本的解决方案。
它会忽略.git和.svn文件夹及其内容。同时不会留下备份文件。
(export LANG=C LC_CTYPE=C
find . -not \( -name .svn -prune -o -name .git -prune \) -type f -print0 | perl -0ne 'print if -T' | xargs -0 sed -Ei 's/[[:blank:]]+$//'
)
L*
变量 - 在子shell中执行。\+
代替*
可以让它更快-否则它会匹配每一行的内容。 - l0b0sed: RE error: illegal byte sequence
错误。 - Brysonexport LANG=C
再试一次。 - Georg Ledermannexport LC_CTYPE=C
,就像在这里找到的一样:https://dev59.com/C2Ik5IYBdhLWcg3we-H5。 - kissgyorgy使用:
find . -type f -print0 | xargs -0 perl -pi.bak -e 's/ +$//'
如果您不想生成“.bak”文件:
find . -type f -print0 | xargs -0 perl -pi -e 's/ +$//'
作为zsh用户,您可以省略对find的调用,而改用以下命令:
as a zsh user, you can omit the call to find, and instead use:
perl -pi -e 's/ +$//' **/*
注意:为了防止破坏 .git
目录,请尝试添加:-not -iwholename '*.git*'
。
.git
文件夹的任何文件夹内运行,无论嵌套多深。 - Illya Moskvinfind . -not \( -name .svn -prune -o -name .git -prune \) -type f -print0 | xargs -0 perl -pi -e 's/ +$//'
- William Dennissrsync -rv --exclude=.git repo/ repo2/
命令来恢复了被损坏的 git 仓库。这样,在 repo
中的本地更改也会出现在(未受损的)repo2
中。 - MatrixManAtYrService.git/index
文件。通常情况下可以使用https://dev59.com/KFYN5IYBdhLWcg3wqpmH#47109640来修复该问题。 - CervEd有两种替代方法,它们也适用于 DOS 换行符 (CR/LF),并且在避免二进制文件方面表现得相当不错:
通用解决方案,检查 MIME 类型是否以 text/
开头:
while IFS= read -r -d '' -u 9
do
if [[ "$(file -bs --mime-type -- "$REPLY")" = text/* ]]
then
sed -i 's/[ \t]\+\(\r\?\)$/\1/' -- "$REPLY"
else
echo "Skipping $REPLY" >&2
fi
done 9< <(find . -type f -print0)
git grep
的 -I
选项跳过 Git 认为是二进制的文件:git grep -I --name-only -z -e '' | xargs -0 sed -i 's/[ \t]\+\(\r\?\)$/\1/'
find dir -type f -exec sed -i 's/ *$//' '{}' ';'
注意:如果您使用的是.git存储库,请尝试添加:-not -iwholename '.git'
。find . -type f -name "*.scss" -regextype posix-extended -not -regex ".*\/(\.|node_modules).*"
。如果,_只有输出看起来完全正常,那么_附加-exec
部分...(自然而然地,在备份文件夹之前进行备份永远不会有害) - Frank N这对我在OSX 10.5 Leopard上运行成功,该系统不使用GNU sed或xargs。
find dir -type f -print0 | xargs -0 sed -i.bak -E "s/[[:space:]]*$//"
如果您有需要排除的文件,请小心处理这个问题(我就遇到了)!
您可以使用-prune来忽略某些目录或文件。对于Git存储库中的Python文件,您可以使用类似以下形式的命令:
find dir -not -path '.git' -iname '*.py'
find dir -not -path '.git' -iname '*.py' -print0 | xargs -0 sed --in-place=.bak 's/[[:space:]]*$//'
后会生成备份文件。请将 dir
替换为要递归的顶级目录名。 - David Gardnersed -i .bak
?难道不应该是sed -i.bak
(没有空格)吗? - Ondra Žižkaack --print0 -l '[ \t]+$' | xargs -0 -n1 perl -pi -e 's/[ \t]+$//'
ack
非常棒。我已经使用它多年了,而且在大多数发行版的软件包仓库中都可以找到。 - Felipe Alvarezex
尝试使用Ex editor(Vim的一部分):
$ ex +'bufdo!%s/\s\+$//e' -cxa **/*.*
** / *。* )。通过 shopt -s globstar 启用。
您可以将以下函数添加到 .bash_profile 中:
# Strip trailing whitespaces.
# Usage: trim *.*
# See: https://dev59.com/mWgv5IYBdhLWcg3wSe62
trim() {
ex +'bufdo!%s/\s\+$//e' -cxa $*
}
sed
使用 sed
,请查看:如何使用sed删除行尾空格?
find
查找以下脚本(例如remove_trail_spaces.sh
),用于从文件中删除行尾空格:
#!/bin/sh
# Script to remove trailing whitespace of all files recursively
# See: https://dev59.com/y3VC5IYBdhLWcg3w51ny
case "$OSTYPE" in
darwin*) # OSX 10.5 Leopard, which does not use GNU sed or xargs.
find . -type f -not -iwholename '*.git*' -print0 | xargs -0 sed -i .bak -E "s/[[:space:]]*$//"
find . -type f -name \*.bak -print0 | xargs -0 rm -v
;;
*)
find . -type f -not -iwholename '*.git*' -print0 | xargs -0 perl -pi -e 's/ +$//'
esac
请在您想要扫描的目录中运行此脚本。在OSX上,它将删除所有以.bak
结尾的文件。
或者只需:
find . -type f -name "*.java" -exec perl -p -i -e "s/[ \t]$//g" {} \;
这是Spring Framework Code Style推荐的方式。
find . -type f -name "*.java" -exec perl -p -i -e "s/[ \t]$//g" {} \;"
只会移除一个尾随空格而不是全部。 - Kalle Richter不要排除文件,以下是一种变化的方法,明确地列出了您想要剥离的文件,根据文件扩展名进行白名单过滤,随意适应您的需要:
find . \( -name *.rb -or -name *.html -or -name *.js -or -name *.coffee -or \
-name *.css -or -name *.scss -or -name *.erb -or -name *.yml -or -name *.ru \) \
-print0 | xargs -0 sed -i '' -E "s/[[:space:]]*$//"
-name "*.rb*"
。 - haroldcarr-name"*.swift"
)来像@haroldcarr所说那样进行递归遍历。 - ettoresed -i '' 's/[[:space:]]*$//g' **/*.*
根据文件树的深度,这个(较短的版本)可能已经满足您的需求。
请注意,它也可以处理二进制文件。
我使用正则表达式。4个步骤:
这将删除所有文件中每行末尾的空格,并且您可以排除一些不符合此要求的文件。