awk is designed for this scenario (among a million others ;-) )
awk -F"\t" -v OFS="\t" '{
for (i=1;i<=NF;i++) {
if ($i == "") $i="NA"
}
print $0
}' file > file.new && mv file.new file
-F="\t"
indicates that the field separator (also known as FS
internally to awk
) is the tab character. We also set the output field separator (OFS)
to "\t"
.
NF
is the number of fields on a line of data. $i
gets evaluated as $1, $2, $3, ...
for each value between 1 and NF
.
We test if the $i th
element is empty with if ($i == "")
and when it is, we change the $i th
element to contain the string "NA".
For each line of input, we print the line's ($0
) value.
Outside the awk
script, we write the output to a temp file, i.e. file > file.new
. The &&
tests that the awk
script exited without errors, and if OK, then moves the file.new
over the original file. Depending on the safety and security use-case your project requires, you may not want to "destroy" your original file.
IHTH.