Here is the solution using awk :
awk '{print; if(getline < "file2") print}' file1
produces this conclusion:
line 1 from file1 line 1 from file2 line 2 from file1 line 2 from file2 ...etc
Using awk can be useful if you want to add additional formatting to the output, for example, if you want to mark each line based on which file it comes from:
awk '{print "1: "$0; if(getline < "file2") print "2: "$0}' file1
produces this conclusion:
1: line 1 from file1 2: line 1 from file2 1: line 2 from file1 2: line 2 from file2 ...etc
Note: this code assumes that file1 has a length greater than or equal to file2.
If file1 contains more lines than file2, and you want to output empty lines for file2 after it completes, add the else clause to the getline test:
awk '{print; if(getline < "file2") print; else print ""}' file1
or
awk '{print "1: "$0; if(getline < "file2") print "2: "$0; else print"2: "}' file1
samgak May 9 '15 at 7:07 a.m. 2015-09-09 07:07 a.m.
source share