how to mixte find -printf and wc -l to create a CSV with files Metadata and number of lines per file

huangapple go评论55阅读模式
英文:

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 &#39;
  for f; do total=$(wc -l &lt; &quot;$f&quot;); printf &quot;%s|&quot; &quot;$total&quot;
done&#39; sh {} \; -printf &#39;%f|%h|%k|%p\n&#39;

huangapple
  • 本文由 发表于 2023年6月12日 13:16:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定