I have the command:
awk 'BEGIN{print "Name, Number"}/value/{print FILENAME, "," $8}' *.txt >> out.csv
Which works perfectly to go through my txt files in the directory, parse the value(s) and write the final csv file with the header (Name, Number).
My issue is that I have "too many" and so I modify it with find and xarg:
find ./ -maxdepth 1 -type f -name '*.txt' | xargs awk 'BEGIN{print "Name, Number"}/value/{print FILENAME, "," $8}' | sed 's/\.\///g' >> out.csv
This has worked in the past, but now I find that -- on occasion -- the header is written more than once to the final csv file. I don't know why. It does ssem to be related to the total number of txt files in the directory such that if I hit a certain number, this happens, but I am not really sure.
thanks.
xargs
runs theawk
command as many times as necessary to avoid the "too many" problem - theBEGIN
block will be executed each time it does so