bashfindwc

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


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 ?


Solution

  • 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'