patternshellMinor
Removing NULL / empty fields
Viewed 0 times
nullemptyremovingfields
Problem
just wanted to check with you could this be done better:
The goal is to print only non-
will print out:
awk -F"\t" '{
for (i = 1; i <= NF; i++) {
if ($i != "NULL") {
printf("%s%s", $i, FS);
}
}
printf("\n");
}' file1The 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 JohnSolution
The same behaviour can be achieved using
Explanation:
-
-
-
After this the only remaining NULL is the one at the beginning of a line (without
sed as follows:echo -e 'TestRecord001\tNULL\tNULL\tAge\t29\tNULL\tNULL\tName\tJohn' | sed '
s/$/\t/;
s/\(\tNULL\)\+\t/\t/g;
s/^NULL\t//';Explanation:
sed s/SearchPattern/Replacement/g. Here s indicates that replacement operation is to be done. Strings matching SearchPattern will be replaced by Replacement. g indicates that the operation will have to be performed on every match and not just on the first occurrence in a line.-
s/$/\t/ adds a tab to the end of each line. [$ matches the end of a line]-
\(\tNULL\)\+\t matches a string of the form \tNULL\tNULL...NULL\t. This is replaced with \t.-
After this the only remaining NULL is the one at the beginning of a line (without
\t to its left). This is matched by ^NULL\t and replaced with the empty string. [^ matches the beginning of a line]Code Snippets
echo -e 'TestRecord001\tNULL\tNULL\tAge\t29\tNULL\tNULL\tName\tJohn' | sed '
s/$/\t/;
s/\(\tNULL\)\+\t/\t/g;
s/^NULL\t//';Context
StackExchange Code Review Q#36201, answer score: 2
Revisions (0)
No revisions yet.