英文:
how to mixte find -printf and wc -l to create a CSV with files Metadata and number of lines per file
问题
我需要从文件中收集一些数据:
/usr$ find . -type f -fprintf /tmp/out.csv "%f | %h | %k | %p\n"
/usr$ head -2 /tmp/out.csv
gettext | ./share/sensible-utils/bin | 4 | ./share/sensible-utils/bin/gettext
inherits_browser.js | ./share/javascript/inherits | 4 | ./share/javascript/inherits/inherits_browser.js
/usr$
在相同的文件上(在现实生活中,这些文件是CSV文件),我使用以下命令来收集行数:
/usr$ find . -type f |while read i;do wc -l "$i" ;done > /tmp/out.csv
/usr$ head /tmp/out.csv
3 ./share/sensible-utils/bin/gettext
27 ./share/javascript/inherits/inherits_browser.js
17 ./share/javascript/typedarray-to-buffer/index.js
0 ./share/javascript/typedarray-to-buffer/index.min.js
297 ./share/javascript/sphinxdoc/1.0/language_data.js
316 ./share/javascript/sphinxdoc/1.0/doctools.js
26 ./share/javascript/sphinxdoc/1.0/theme_extras.js
514 ./share/javascript/sphinxdoc/1.0/searchtools.js
158 ./share/javascript/sphinxdoc/1.0/sidebar.js
1 ./share/javascript/sphinxdoc/1.0/css3-mediaqueries.js
/usr$
我如何生成一个包含-fprintf
的所有列以及wc -l
输出的/tmp/out.csv
文件?
英文:
I need to collect some data from files:
/usr$ find . -type f -fprintf /tmp/out.csv "%f | %h | %k | %p\n"
/usr$ head -2 /tmp/out.csv
gettext | ./share/sensible-utils/bin | 4 | ./share/sensible-utils/bin/gettext
inherits_browser.js | ./share/javascript/inherits | 4 | ./share/javascript/inherits/inherits_browser.js
/usr$
On the sames files (in real life this files are CSV) I collect the number of lines with:
/usr$ find . -type f |while read i;do wc -l "$i" ;done > /tmp/out.csv
/usr$ head /tmp/out.csv
3 ./share/sensible-utils/bin/gettext
27 ./share/javascript/inherits/inherits_browser.js
17 ./share/javascript/typedarray-to-buffer/index.js
0 ./share/javascript/typedarray-to-buffer/index.min.js
297 ./share/javascript/sphinxdoc/1.0/language_data.js
316 ./share/javascript/sphinxdoc/1.0/doctools.js
26 ./share/javascript/sphinxdoc/1.0/theme_extras.js
514 ./share/javascript/sphinxdoc/1.0/searchtools.js
158 ./share/javascript/sphinxdoc/1.0/sidebar.js
1 ./share/javascript/sphinxdoc/1.0/css3-mediaqueries.js
/usr$
How could I produce an /tmp/out.csv
file with all the columns from the -fprintf
plus one with the output of wc -l
?
答案1
得分: 2
类似这样的吗?
find /path -type f -exec sh -c ''
for f; do total=$(wc -l < "$f"); printf "%s|" "$total"
done' sh {} ; -printf ''%f|%h|%k|%p\n'
----
- 参考 [了解 find 命令的 -exec 选项][1]
[1]: https://unix.stackexchange.com/questions/389705/understanding-the-exec-option-of-find
英文:
Something like this?
find /path -type f -exec sh -c '
for f; do total=$(wc -l < "$f"); printf "%s|" "$total"
done' sh {} \; -printf '%f|%h|%k|%p\n'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论