Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have input.txt tab-delimited text file around 30K lines, I would like to check each row (s1..s30K lines) for missing value (i.e blank space) and fill the missing value with zero value.See out.txt


 id  no1  no2  no3  no4
 s1  23   34   45   12
 s2       4    4      
 s3  4         8    0


id  no1  no2  no3  no4
s1  23   34   45   12
s2  0     4    4    0  
s3  4     0    8    0
share|improve this question
up vote 6 down vote accepted

You could do it like this with awk:

awk 'BEGIN { FS = OFS = "\t" } { for(i=1; i<=NF; i++) if($i ~ /^ *$/) $i = 0 }; 1' file


Setting FS and OFS to tab ensures the output is correctly delimited. The for-loop looks at every field and set it to zero if they're empty. The one at the end is a shorthand for { print $0 }.

share|improve this answer

I'd prefer:

sed 's/<TAB> /<TAB>0/g' <input.txt >output.txt

Replace <TAB> with the real TAB character (generally obtained by hitting Ctrl-V, then Tab)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.