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
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250

3 Answers3

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
RomanPerekhrest
  • 29,703
  • 3
  • 43
  • 67
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
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
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

dgfjxcv
  • 656
  • 3
  • 13