Search code examples
awkpastecut

paste command leaves empty rows


Hi I was trying to paste multiple files (each with a single column but different number of rows) together.

paste file1.txt file2.txt paste3.txt ... paste100 > out.txt

input file 1:

A
B
C

input file 2:

D
E

input file 3:

F
G
H
I
J

output:

A   D   F
B   E   G
C       H
        I
        J

When I cut column 2 (cut -f2) from out.txt file , it gives column 2 with 3 empty rows (probably because column has 5 rows so to match to column 3, it created 2 extra empty rows). Same goes for column 1 (less out.txt | cut -f1) which gives 2 empty rows. Any ideas why does it show the empty rows?

less out.txt | cut -f1

A
B
C
empty cell
empty cell

less out.txt | cut -f2

D
E
empty cell
empty cell
empty cell

I was expecting to see-

column 1

A
B
C  

column 2

D
E

Solution

  • None of the rows are empty, some of them just don't have all of the fields populated but they do still have the field separators (tabs) that paste output. cut has no way of knowing that you don't want the empty fields printed.

    Try:

    awk -v f=1 -F'\t' '$f!=""{print $f}' file
    awk -v f=2 -F'\t' '$f!=""{print $f}' file
    

    instead.