appending data from similar files

I am familiar with scripting, but I am trying to see if there is an easy way to append files from similar files into one file. For example, if there is file1_20121201, file1_20121202, file1_20121203,
file2_20121201, file2_20121202, file2_20121203

I want to be able to combine all the data from file1_* into file1_output and file2_* into file2_output.

Basically, appending the data from all similar files into one file. I can probably try doing the hard way of looping through all the files, but I am sure there will be much easier and flexible way (not depending on the number of files based on a certain pattern, etc).

Any help on this is greatly appreciated.

Thanks

One way:

for x in 1 2
do
    (ls file${$x}_* | xargs cat ) >file${x}_output
done

Not tested!!

1 Like

Sorry, I should have been more specific...I am not sure how many such patterns exist. In other words, it could be file1_, file2_, file3_* ...filen_*
how can I do this dynamically?
The actual situation in my case is the timestamp is appended to file1, file2, etc. I would like to combine all such (file1_, file2_) from a current day (or whatever the correct range of files I determine) to be appended into one file. But if I get a general idea, I can proceed with the actual implementation, except that I am stuck how to do this dynamically.

for x in `ls | egrep "file[0-9]+.*"`; do n=`echo $x | cut -d_ -f1 | sed 's/file//'`; cat $x >> file${n}_output; done
1 Like