如何将制表符转换为一个目录的每个文件中的空格(可能递归)?
此外,是否有一种方法来设置每个制表符的空格数?
如何将制表符转换为一个目录的每个文件中的空格(可能递归)?
此外,是否有一种方法来设置每个制表符的空格数?
当前回答
从Gene的回答中收集最好的评论,到目前为止最好的解决方案是使用moreutils的海绵。
sudo apt-get install moreutils
# The complete one-liner:
find ./ -iname '*.java' -type f -exec bash -c 'expand -t 4 "$0" | sponge "$0"' {} \;
解释:
./从当前目录递归搜索 -iname是不区分大小写的匹配(对*.java和*.java都适用) Type -f只查找常规文件(没有目录、二进制文件或符号链接) -exec bash -c在子shell中为每个文件名执行以下命令,{} expand -t 4将所有tab扩展为4个空格 海绵吸收标准输入(从扩展)并写入文件(同一文件)*。
注意:*一个简单的文件重定向(> "$0")在这里不能工作,因为它会很快覆盖文件。
优点:保留所有原始文件权限,不使用中间的tmp文件。
其他回答
将制表符转换为空格只是在”。Lua " files [tab -> 2个空格]
find . -iname "*.lua" -exec sed -i "s#\t# #g" '{}' \;
对于递归应用程序,我喜欢上面的“find”示例。为了使其具有非递归性,只更改当前目录中匹配通配符的文件,shell glob扩展可以满足少量文件的需求:
ls *.java | awk '{print "expand -t 4 ", $0, " > /tmp/e; mv /tmp/e ", $0}' | sh -v
如果在您相信它工作之后希望它保持沉默,只需在sh命令的末尾加上-v。
当然,您可以在第一个命令中选择任何一组文件。例如,以受控的方式只列出一个特定的子目录(或多个目录),如下所示:
ls mod/*/*.php | awk '{print "expand -t 4 ", $0, " > /tmp/e; mv /tmp/e ", $0}' | sh
或者反过来运行find(1)与深度参数等的一些组合:
find mod/ -name '*.php' -mindepth 1 -maxdepth 2 | awk '{print "expand -t 4 ", $0, " > /tmp/e; mv /tmp/e ", $0}' | sh
在发现混合制表符和空格后,我使用style重新缩进所有的C/ c++代码。如果您愿意,它还可以强制使用特定的括号样式。
在其他答案中建议的使用expand似乎是仅用于此任务的最合乎逻辑的方法。
也就是说,它也可以用Bash和Awk来完成,以防你想在它的同时做一些其他的修改。
如果使用Bash 4.0或更高版本,shopt内置的globstar可以使用**进行递归搜索。
在GNU Awk 4.1或更高版本中,sed可以像“inplace”一样修改文件:
shopt -s globstar
gawk -i inplace '{gsub("\t"," ")}1' **/*.ext
如果你想设置每个制表符的空格数:
gawk -i inplace -v n=4 'BEGIN{for(i=1;i<=n;i++) c=c" "}{gsub("\t",c)}1' **/*.ext
我的建议是使用:
find . -name '*.lua' -exec ex '+%s/\t/ /g' -cwq {} \;
评论:
Use in place editing. Keep backups in a VCS. No need to produce *.orig files. It's good practice to diff the result against your last commit to make sure this worked as expected, in any case. sed is a stream editor. Use ex for in place editing. This avoids creating extra temp files and spawning shells for each replacement as in the top answer. WARNING: This messes with all tabs, not only those used for indentation. Also it does not do context aware replacement of tabs. This was sufficient for my use case. But might not be acceptable for you. EDIT: An earlier version of this answer used find|xargs instead of find -exec. As pointed out by @gniourf-gniourf this leads to problems with spaces, quotes and control chars in file names cf. Wheeler.