Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have files with naming convention of this pattern:


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?

share|improve this question
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 2 '12 at 18:48

2 Answers 2

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

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

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

share|improve this answer
Thank you thanks alot it worked.thanks once again. –  rajitha Jun 3 '12 at 5:48
printf "%s\n" * | cut -d. -f1-3 | sort -u | while read prefix; do
  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"
share|improve this answer
Thank you. It worked. thanks alot for ur help. –  rajitha Jun 3 '12 at 5:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.