Skip to main content
edited tags
Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
add awk tag.
Link
rolfl
  • 98.2k
  • 17
  • 220
  • 419

Remove Removing NULL / empty fields using awk

Source Link
robosoul
  • 217
  • 3
  • 6

Remove NULL / empty fields using awk

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