Skip to main content
3 of 3
edited tags
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Removing NULL / empty fields

just wanted to check with you could this be done better:

awk -F"\t" '{
    for (i = 1; i <= NF; i++) {
        if ($i != "NULL") {
            printf("%s%s", $i, FS);
        }
    }
   
    printf("\n");
}' file1

The goal is to print only non-NULL fields. For example:

echo "TestRecord001 NULL    NULL    Age 29  NULL    NULL    Name    John" | awk -F"\t" '{
    for (i = 1; i <= NF; i++) {
        if ($i != "NULL") {
            printf("%s%s", $i, FS);
        }
    }
   
    printf("\n");
}'

will print out: TestRecord001 Age 29 Name John

robosoul
  • 217
  • 3
  • 6