Thursday, July 5, 2012

Read 2 files line by line alternatively



 In this article, we will see different ways in which we can read 2 files in parallel line by line, i.e, one line from file1 and one line from file2, next line from file 1, next line from file2, and so on. This way of reading produces a combined file output in which lines are alternated between the files.

Let us consider 2 sample files with the following contents:
$ cat file1
Unix
Linux
Solaris
$ cat file2
AIX
HPUX
Cygwin
1. The paste command is the easiest of all to read 2 files simultaneously.
$ paste -d"\n" file1 file2
Unix
AIX
Linux
HPUX
Solaris
Cygwin
     paste prints the contents of multiple files side by side. By using the delimiter(-d) as the newline, the lines come to the new line.

2. awk option.
$ awk '1;{getline < x;}1' x="file2" file1
Unix
AIX
Linux
HPUX
Solaris
Cygwin
      The variable x is assigned the 2nd filename "file2". The '1' command prints the lines of file1. getline command reads the contents of file2 and prints one line at a time. In this fashion, the lines of both the files get printed in alternate fashion.

3. Using paste with awk:
$ paste file1 file2 | awk '$1=$1' OFS='\n'
Unix
AIX
Linux
HPUX
Solaris
Cygwin
      awk uses newline as the output field separator(OFS) and hence the lines from both the files come in alternate lines.

4. paste output is piped to the while loop.
$ paste file1 file2 | while read f1 f2
> do
>   echo $f1
>   echo $f2
> done
Unix
AIX
Linux
HPUX
Solaris
Cygwin
    The output of paste command is piped to the while loop. It reads lines from file1 into f1 variable, file2 to f2 variable, and prints them inside.

Note: This option will work only if your file contains only one word.

5. A pure shell solution.
#!/bin/bash
exec 3< file1
exec 4< file2
while :
do
        read x <&3
        read y <&4
        [ -n "$x" ] && echo $x
        [ -n "$y" ] && echo $y
        [  -z "$x" -a -z "$y" ] && break
done
     2 file descriptors 3 and 4 are assigned to file1 to file2. Using the read command, the files are read one by one and they are printed. The loop is quit when there are no more lines to read in either files. The ':' in the while loop indicates a loop without any condition. This solution uses only internal commands.

No comments:

Post a Comment