0

I have two massive text files containing more than 10000 lines.

I need to make from them a new file that contains a line from each file.


File1 

  line a
  line b
  line c

File2

   line 1
   line 2
   line 3

output

New File

   line a
   line 1
   line b
   line 2
   line c
   line 3
0

3 Answers 3

2

Simply with paste command:

paste -d'\n' file1 file2 > result
  • -d'\n' - use newline character \n as delimiter between corresponding merged items/lines

$ cat result
line a
line 1
line b
line 2
line c
line 3
1

Using awk:

$ awk '1; { getline <"file1" } 1' file2
line a
line 1
line b
line 2
line c
line 3

or, more verbosely,

$ awk '{ print; getline <"file1"; print }' file2
line a
line 1
line b
line 2
line c
line 3
0
0

Use file descriptors

open two file descriptors and read from them

while read -r -u 4 l1 && read -r -u 5 l2 ; do
echo $l1
echo $l2
done 4<file1 5<file2 > new_file

read man

Not the answer you're looking for? Browse other questions tagged .