paste
is the way to go, but for the sake of completeness here is a bash
focused solution (with little help from seq
, and wc
):
for _ in $(seq 1 $(wc -l <f1.txt)); do \
read -u 3 one; read -u 4 two; echo "${one}${two}"; done 3<f1.txt 4<f2.txt
Here we are sending the contents of files f1.txt
, and f2.txt
via file descriptors 3, and 4. read
reads from the respective FDs, and echo
prints the output in desired format.
The iteration will stop after the f1.txt
ends, here _
is a throwaway variable.
Better, just bash
, depending on read
's exit status when the EOF is reached, with a while
construct (thanks to steeldriver):
while IFS= read -ru3 one && IFS= read -ru4 two; do echo "${one}${two}"; done 3<f1.txt 4<f2.txt
Example:
$ cat f1.txt
AAAA
BBBB
CCCC
$ cat f2.txt
XXXX
YYYY
ZZZZ
$ for _ in $(seq 1 $(wc -l <f1.txt)); do read -u 3 one; read -u 4 two; echo "${one}${two}"; done 3<f1.txt 4<f2.txt
AAAAXXXX
BBBBYYYY
CCCCZZZZ
$ while IFS= read -ru3 one && IFS= read -ru4 two; do echo "${one}${two}"; done 3<f1.txt 4<f2.txt
AAAAXXXX
BBBBYYYY
CCCCZZZZ