Hi,
I need to concatenate data files with a .mp extension that are stored in directories by year. I want to keep the same filename as an output for example:
for the file name p030.mp, which resides in the following subdirectories:
/2000/p030.mp
/2001/p030.mp
/2002/p030.mp
I want to:
concatenate p030.mp for 2000-2002 and output the concatenated file in a new directory, say:
/ifeeldumb/p030.mp
I can get this to work for a single file:
cat /2000/p030.mp /2001/p030.mp /2002/p030.mp > p030.mp
but I get lost when I try to put it in a loop and extract individual filenames for use with output names.
Thanks for your reply. To be more specific, p030.mp is the name of a station for GPS data. So, in each subdirectory by year, I have probably 50 different station names with the .mp extension. The only thing the station names (filenames) have in common, besides the .mp extension, is that they can only be 4 letter/number combos in length proceeding the .mp extension:
so they could be:
p030.mp
abcd.mp
abc0.mp
iid2.mp
To make it more confusing, I have data from 2006-2010 but some stations only have data from 2008-onward.
In terms of actual content within the station files, they may have different lines (different days of year present). Not sure if that matters.
Something like this should work (assuming that your present working directory contains the 20?? directories.)
#!/usr/bin/env ksh
target=/tmp/cats
find 20?? -name "*.mp" | while read file
do
base=${file##*/} # ditch the directory portion of the filename
cat $file >>$target/$base.new # I like to add .new to prevent accidental overwriting while testing!
done
Change target to the directory you want to put the output files into and you're good to go.