Search code examples
awkprintingis-empty

Remove empty columns by awk


I have a input file, which is tab delimited, but I want to remove all empty columns. Empty columns : $13=$14=$15=$84=$85=$86=$87=$88=$89=$91=$94

INPUT: tsv file with more than 90 columns

a b   d e   g...  
a b   d e   g...

OUTPUT: tsv file without empty columns

a b d e g....
a b d e g...

Thank you


Solution

  • This might be what you want:

    $ printf 'a\tb\tc\td\te\n'
    a       b       c       d       e
    
    $ printf 'a\tb\tc\td\te\n' | awk 'BEGIN{FS=OFS="\t"} {$2=$4=""} 1'
    a               c               e
    
    $ printf 'a\tb\tc\td\te\n' | awk 'BEGIN{FS=OFS="\t"} {$2=$4=RS; gsub("(^|"FS")"RS,"")} 1'
    a       c       e
    

    Note that the above doesn't remove all empty columns as some potential solutions might do, it only removes exactly the column numbers you want removed:

    $ printf 'a\tb\t\td\te\n'
    a       b               d       e
    
    $ printf 'a\tb\t\td\te\n' | awk 'BEGIN{FS=OFS="\t"} {$2=$4=RS; gsub("(^|"FS")"RS,"")} 1'
    a               e