merge two files, first line from first file followed by first line from second file
It's a job for paste
:
paste -d'\n' f1.txt f2.txt
Example:
$ cat foo.txt
some text line 1
some text line 2
some text line 3
$ cat bar.txt
A1
A2
A3
$ paste -d'\n' foo.txt bar.txt
some text line 1
A1
some text line 2
A2
some text line 3
A3
Yes, you can do this using one while loop and read through the two files using read
.
#!/bin/sh
while read file1 <&3 && read file2 <&4
do
printf "%s\n" "$file1" >> mergedFile.txt
printf "%s\n" "$file2" >> mergedFile.txt
done 3</path/to/file1/file1.txt 4</path/to/file2/file2.txt
You can use echo
instead of printf
. The results are in mergedFile.txt If the files you are processing are not huge, perhaps the above is easier and more portable than most solutions.