I need to add header (single line) to huge (>10k) number of text files. Let as assume that the variable $HEADER does contain appropriate header. The command
find -type f -name 'tdgen_2012_??_??_????.csv' | xargs sed -i "1s/^/$HEADER\n/"
does work well. The problem I face is that some of the data files (tdgen_2012_????????.csv) is empty. The sed(1) cannot address non exist line of the file. I decided to manage empty files in separate way:
echo $HEADER | tee $(find -type f -name 'tdgen_2012_??_??_????.csv' -empty) > /dev/null
Due to amount of empty files the command above does not work. The tee(1) cannot write to unlimited count of the files. Also the number of the command line arguments can be exceeded.
I do not want to use the for-cycle due to low performance (the tee(1) can write many files at once).
My questions:
- Does exist one solution for both kind of data files (empty/non-empty) at once?
- If not: how to manage empty files effectively?