将wc行附加到文件名 [英] Append wc lines to filename
本文介绍了将wc行附加到文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
标题说明了一切.我已经设法做到这一点:
Title says it all. I've managed to get just the lines with this:
lines=$(wc file.txt | awk {'print $1'});
但是我可以使用辅助将其附加到文件名中.向我展示如何在当前目录中的所有.txt文件中循环显示的要点.
But I could use an assist appending this to the filename. Bonus points for showing me how to loop this over all the .txt files in the current directory.
推荐答案
find -name '*.txt' -execdir bash -c \
'mv -v "$0" "${0%.txt}_$(wc -l < "$0").txt"' {} \;
其中
- 为每个(
\;
)匹配文件执行bash
命令; -
{}
替换为当前处理的文件名,并作为第一个参数($0
)传递给脚本; -
${0%.txt}
从字符串的后面删除.txt
的最短匹配项(请参见仅打印文件中的行数(请参阅对
- the
bash
command is executed for each (\;
) matched file; {}
is replaced by the currently processed filename and passed as the first argument ($0
) to the script;${0%.txt}
deletes shortest match of.txt
from back of the string (see the official Bash-scripting guide);wc -l < "$0"
prints only the number of lines in the file (see answers to this question, for example)
示例输出:
'./file-a.txt' -> 'file-a_5.txt'
'./file with spaces.txt' -> 'file with spaces_8.txt'
这篇关于将wc行附加到文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文