Question: 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?

Answer: 
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.
```
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 

```