1

I have many files with the following names:

      0001mm.xyz  , 0002mm.xyz , 0003mm.xyz ...
      0001qm.xyz  , 0002qm.xyz , 0003qm.xyz ...

I want to join the contents of 0001mm.xyz and 0001qm.xyz to produce a 0001.xyz ; 0002mm.xyz and 0002qm.xyz to produce a 0002.xyz, and so on.

1
  • Useful use of cat?
    – pfnuesel
    Commented Apr 23, 2016 at 0:07

1 Answer 1

1

To join two files, use cat.

cat 0001mm.xyz 0001qm.xyz >0001mm.xyz

To iterate over the input files, use a for loop. Loop over one of the input files, using a wildcard pattern.

for x in *mm.xyz; do … done

Given a file $x ending with mm.xyz, to get the name of the other input file and the name of the output file, use parameter expansion constructs to extract the stem.

for x in *mm.xyz; do
  stem="${x%mm.xyz}"
  cat "$x" "${stem}qm.xyz" >"${stem}.xyz"
done
2
  • I used your final script but an error happened, it not found the qm.xyz file
    – alloppp
    Commented Apr 23, 2016 at 2:02
  • @AlvaroLopez I'd made a mistake that Archemar fixed (thanks Archemar), see the edited answer. Commented Apr 23, 2016 at 12:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .