用零替换缺失值空白


13

我有大约30K行的制表符分隔文本文件input.txt,我想检查每行(s1..s30K行)是否有缺失值(即空白),并用零值填充缺失值。请参见out.txt

input.txt

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

out.txt

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

Answers:


14

您可以使用awk这样做:

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

说明

将FS和OFS设置为制表符可确保正确定界输出。for循环查看每个字段,如果为空,则将其设置为零。最后的一个是的简写{ print $0 }


5

我更喜欢:

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

替换<TAB>为真实的TAB字符(通常是按Ctrl-V,然后按Tab

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.