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.

  • Useful use of cat? – pfnuesel Apr 23 '16 at 0:07
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
| improve this answer | |
  • I used your final script but an error happened, it not found the qm.xyz file – alloppp Apr 23 '16 at 2:02
  • @AlvaroLopez I'd made a mistake that Archemar fixed (thanks Archemar), see the edited answer. – Gilles 'SO- stop being evil' Apr 23 '16 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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