-1

I have a text file (A.txt) with this these lines:

Aaaaaaa1
Aaaaaaa2
Aaaaaaa3

And other text file (B.txt) with this these lines:

Bbbbbbb1
Bbbbbbb2
Bbbbbbb3

I would like to combine A.txt and B.txt in other file (OUTCOME.txt) in this way:

Aaaaaaa1
Bbbbbbb1
Aaaaaaa2
Bbbbbbb2
Aaaaaaa3
Bbbbbbb3

How could I make this using bash shell in linux using read, grep, echo, etc?

4 Answers4

2

With GNU sed:

sed 'R b.txt' a.txt > OUTCOME.txt

Output to OUTCOME.txt:

Aaaaaaa1
Bbbbbbb1
Aaaaaaa2
Bbbbbbb2
Aaaaaaa3
Bbbbbbb3
Cyrus
  • 84,225
  • 14
  • 89
  • 153
2

Just paste is enough:

$ cat file1
Aaaaaaa1
Aaaaaaa2
Aaaaaaa3

$ cat file2
Bbbbbbb1
Bbbbbbb2
Bbbbbbb3


$ paste -d $'\n' file1 file2
Aaaaaaa1
Bbbbbbb1
Aaaaaaa2
Bbbbbbb2
Aaaaaaa3
Bbbbbbb3
George Vasiliou
  • 6,130
  • 2
  • 20
  • 27
  • I am doing exercises on using echo, how can I do it the exact same way, but using echo ? – Bich Le The Jan 07 '18 at 22:15
  • @LenyNguyenová I don't think that you can get the same result easily with echo. You need to built some loops to read each line of each file and echo them alternativelly. – George Vasiliou Jan 07 '18 at 22:55
1

Using echo and read:

while IFS= read -r line1 && IFS= read -ru3 line2; do
    echo "$line1"
    echo "$line2"
done < file1 3< file2

It's important that the second read uses a different file descriptor (3 in this case).

PesaThe
  • 7,259
  • 1
  • 19
  • 43
0

paste + awk solution (for your current input files):

paste a.txt b.txt | awk '{ print $1 ORS $2 }' > outcome.txt
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105