How can i merge these two files into several...

Given are File A and File B
File A has for example 5 lines:
AAA
BBB
CCC
DDD
EEE

File B has 3 lines:
111
222
333

How can i merge A and B into:
111
222
333
AAA (first line from A)

then a new file:
111
222
333
BBB

etc.
so that in the end i have 5 new files, where the first three lines are from File B, and the last, 4th line, is always "the next" from File A, and also, how can these files be named automatically, like File1, File2, File3...

Thank you very much in advance, every help is much appreciated!

Try something like this:

i=1
while read -r line; do
  printf "%s\n" "$line" | cat fileB - > file$i
  i=$((i+1))
done < fileA

or

i=1
while read -r line; do
  cp fileB file$i
  printf "%s\n" "$line" >> file$i
  i=$((i+1))
done < fileA
1 Like

wow. thank you! that was fast. and works! you saved me a lot of time...
May i ask a second question?
How can i have the new file named like the added line? So that it is not File1, but "AAA" as filename?

Try this:

while read -r line
do
  printf "%s\n" "$line" | cat fileB - > "$line"
done < fileA
1 Like

you are a star! when i find out how to add a "thank you" in your statistics that i see on the right, i will certainly do so!
Thank you!