1

I have files with naming convention of this pattern:

bond_7.LEU.CA.1.dat
bond_7.LEU.CA.2.dat
bond_7.LEU.CA.3.dat
bond_12.ALA.CB.1.dat
bond_12.ALA.CB.2.dat
bond_12.ALA.CB.3.dat
...

I want to concatenate all files of the same group into a single one. For example:

cat bond_7.LEU.CA.*.dat > ../bondvalues/bond_7.LEU.CA.1_3.dat

There's large number of these files. How can achieve this with a bash script?

Oliver Salzburg
  • 87,539
  • 63
  • 263
  • 308
rajitha
  • 11
  • Are there always exactly three files in a given group? If the number of files is variable, does it have to be bond_7.LEU.CA.1_3.dat or would bond_7.LEU.CA.dat also do? The latter is easier. – Dennis Jun 02 '12 at 18:48

2 Answers2

2

Assuming that the example you provide reflects all of your files the following should do the trick:

for f in *.1.dat
do
  cat ${f%%1.dat}* > ${f%%1.dat}1_3.dat
done

This requires that each group contains a file with the .1.dat extension.

cYrus
  • 21,787
Bram
  • 622
0
printf "%s\n" * | cut -d. -f1-3 | sort -u | while read prefix; do
  files=(${prefix}*)
  first=$(cut -d. -f4 <<< "${files[0]}")
  last=$(cut -d. -f4 <<< "${files[${#files[@]}-1]}")
  newfile=$(printf "../bondvalues/%s.%s_%s.dat" "$prefix" "$first" "$last")
  cat "${files[@]}" > "$newfile"
done 
glenn jackman
  • 26,306