Friday, June 1, 2018

bash - concatenate files including path in header - path contains spaces


I have to concatenate a number of files in a directory structure which contains spaces in the folder names looking like this: ./CH 0000100014/A10/11XT/11xt#001.csv


find . -name "*.csv" -type f -print0 | xargs -0  cat > allmycsv.txt

does the job, however now I need to include the information contained in the path, i.e. CH 0000100014/A10/11XT as a header of each inputfile to cat.


find . -name "*.csv" -type f -print0 | xargs -0 -I % sh -c 'echo %; cat %' >allmycsv.txt

would do the job, if I had no spaces in the path, but in my case, cat does not get along with the space in the path name. Is there a way out?


Cheers,
E


P.S. I am working on bash on OSX


Answer



You can read the file list in a while loop (see BashFAQ #020):


find . -name "*.csv" -type f -print0 | while IFS= read -d $'\0' -r file ; do
echo "$file"
cat "$file"
done >allmycsv.txt

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...