Search code examples
awkpasteblank-line

Remove lines from a file corresponding to blank lines of another file


I have two files with same amount of rows and columns. Delimited with ;. Example;

file_a:

1;1;1;1;1
2;2;2;2;2
3;3;3;3;3
4;4;4;4;4

file_b:

A;A;A;A;A
B;B;;;B
;;;;
D;D;D;D;D

Ignoring delimiters, line 3 is empty from file_b. So I want to remove line 3 from file_a as well, before command;

paste -d ';' file_a file_b.

in order to have an output like this:

1;1;1;1;1;A;A;A;A;A
2;2;2;2;2;B;B;;;B
4;4;4;4;4;D;D;D;D;D

Edit: Number of columns is 93 and same for each row and for both files, so both files have exactly the same matrix of rows and columns.


Solution

  • Could you please try following, written and tested with shown samples in GNU awk.

    awk '
    BEGIN{
      FS=OFS=";"
    }
    FNR==NR{
      arr[FNR]=$0
      next
    }
    !/^;+$/{
      print arr[FNR],$0
    }
    ' file_a file_b
    

    Explanation: Adding detailed explanation for above.

    awk '                 ##Starting awk program from here.
    BEGIN{                ##Starting BEGIN section from here.
      FS=OFS=";"          ##Setting field separator and output field separator as ; here.
    }
    FNR==NR{              ##Checking condition if FNR==NR which will be TRUE when file_a is being read.
      arr[FNR]=$0         ##Creating arr with index FNR and value is current line.
      next                ##next will skip all further statements from here.
    }
    !/^;+$/{              ##Checking condition if line NOT starting from ; till end then do following.
      print arr[FNR],$0   ##Printing arr with index of FNR and current line.
    }
    ' file_a file_b       ##Mentioning Input_file names here.