PDA

View Full Version : Help with Shell Script Needed


infinity2030
6th March 2009, 07:23
Hi all,

I'm a newbie. I'm thinking of making a shell script which will list a directory for *.csv files and merge all the files into a single file called data.csv.

However, i have no idea how to. What i do currently is to manually issue the following command to merge multiple CSV files into one:-

cat file1.csv >> data.csv
cat file2.csv >> data.csv
etc..

I need a script to help me in doing so, so i need not worry about what file name would the csv be, and they'll all be merged into a single data.csv whenever i execute the script.

Can anyone help? Thanks alot!

falko
7th March 2009, 14:47
Try something like this:

for file in "$( /usr/bin/find /home/sqlbackup -name *.csv -type f )"
do
cat $file >> data.csv
done

See man find for more details.

athmane
7th March 2009, 21:04
Try this (if the order is not important) and after cd-ing to your directory:

cat `find . -name "*.csv" 2> /dev/null` > data.csv