edg
edg

Reputation: 353

Concatenating files with a different name but the same number in a folder with muliple files

I am very new to Shell/Bash but I want to use it to establish a pipeline for some analyses. I have used bash to generate multiple files like this:

for i in {1..10}; 
    do sim XX.in.$i.txt > XX.out.$i.txt;
    done;

for i in {1..10}; 
    do sim YY.in.$i.txt > YY.out.$i.txt;
    done;

which gives me 20 outputfiles; XX.out.1.txt, XX.out.2.txt, YY.out.1.txt, YY.out.2.txt etc.

Now I want to concatinate XX.out.1.txt and YY.out.1.txt and then XX.out.2.txt and YY.out.2.txt etc, so always only two files with different names but the same NUMBER. What is the easiest way to do this?

Upvotes: 0

Views: 97

Answers (2)

twalberg
twalberg

Reputation: 62519

You can avoid repeating the loop:

for i in {1..10}; do
  ( sim XX.in.${i}.txt; sim YY.in.${i}.txt ) > concatenated.${i}.txt
done

If you need to keep the intermediate files, though:

for i in {1..10}; do
  sim XX.in.${i}.txt > XX.out.${i}.txt
  sim YY.in.${i}.txt > YY.out.${i}.txt
  cat XX.out.${i}.txt YY.out.${i}.txt > concatenated.${i}.txt
done

Upvotes: 1

choroba
choroba

Reputation: 242433

The solution is very similar to how you created the files:

for i in {1..10} ; do
    cat XX.out.$i.txt YY.out.$i.txt > concatenated.$i.txt
done

Upvotes: 0

Related Questions