find - How to add header to huge amount of files (empty/non-empty) -


i need add header (single line) huge (>10k) number of text files. let assume variable $header contain appropriate header. command

find -type f -name 'tdgen_2012_??_??_????.csv' | xargs sed -i "1s/^/$header\n/" 

does work well. problem face of data files (tdgen_2012_????????.csv) empty. sed(1) cannot address non exist line of file. decided manage empty files in separate way:

echo $header | tee $(find -type f -name 'tdgen_2012_??_??_????.csv' -empty) > /dev/null 

due amount of empty files command above not work. tee(1) cannot write unlimited count of files. number of command line arguments can exceeded.

i not want use for-cycle due low performance (the tee(1) can write many files @ once).

my questions:

  1. does exist 1 solution both kind of data files (empty/non-empty) @ once?
  2. if not: how manage empty files effectively?

echo $header > header find -type f -name 'tdgen_2012_??_??_????.csv' \     -exec sh -c '{ echo $header; cat {}; } > tmp && mv tmp {}' \; -print 

explanation:

1. -exec sh -c "..." - able call more 1 command

2. { echo $header; cat {}; } > tmp && mv tmp {} - concatenate $header , found file tmp , rename tmp found file. because can't cat header {} > {}

3. -print - show filename of every changed file


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -