How can I merge files on a line by line basis?

The right tool for this job is probably paste

paste -d '' file1 file2

See man paste for details.


You could also use the pr command:

pr -TmJS"" file1 file2

where

  • -T turns off pagination
  • -mJ merge files, Joining full lines
  • -S"" separate the columns with an empty string

If you really wanted to do it using pure bash shell (not recommended), then this is what I'd suggest:

while IFS= read -u3 -r a && IFS= read -u4 -r b; do 
  printf '%s%s\n' "$a" "$b"
done 3<file1 4<file2

(Only including this because the subject came up in comments to another proposed pure-bash solution.)


Through awk way:

awk '{getline x<"file2"; print $0x}' file1
  • getline x<"file2" reads the entire line from file2 and holds into x variable.
  • print $0x prints the whole line from file1 by using $0 then x which is the saved line of file2.