Take the 2-minute tour ×
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:

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?

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
add comment

2 Answers

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 
share|improve this answer
    
Thank you. It worked. thanks alot for ur help. –  rajitha Jun 3 '12 at 5:49
add comment

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.

share|improve this answer
    
Thank you thanks alot it worked.thanks once again. –  rajitha Jun 3 '12 at 5:48
add comment

Your Answer

 
discard

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.