标题说明了一切。我已经设法得到这样的行:
lines=$(wc file.txt | awk {'print $1'});
但我可以使用辅助功能将其附加到文件名中。向我展示如何循环遍历当前目录中的所有 .txt 文件的奖励积分。
find -name '*.txt' -execdir bash -c \
'mv -v "$0" "${0%.txt}_$(wc -l < "$0").txt"' {} \;
where
- the
bash
为每个 (\;
) 匹配的文件;
-
{}
被当前处理的文件名替换并作为第一个参数传递($0
) 到脚本;
-
${0%.txt}
删除最短匹配.txt
从字符串的后面(参见官方 Bash 脚本指南 http://tldp.org/LDP/abs/html/string-manipulation.html);
-
wc -l < "$0"
仅打印文件中的行数(请参阅答案这个问题 https://stackoverflow.com/questions/10238363/how-to-get-wc-l-to-print-just-the-number-of-lines-without-file-name, 例如)
示例输出:
'./file-a.txt' -> 'file-a_5.txt'
'./file with spaces.txt' -> 'file with spaces_8.txt'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)