Filling space/tab separated, empty columns with 0

前端 未结 5 2049
天命终不由人
天命终不由人 2021-01-07 02:06

i have a huge file and as an output some columns doesn\'t have a value, i need to fill these columns with 0 for further analysis. I can separate the columns with space or ta

5条回答
  •  甜味超标
    2021-01-07 02:34

    If and only if your data only contains numbers and you have clear defined field separator FS, you can use the following trick:

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

    By adding zero, we convert strings to numbers. Empty strings will be converted to the number zero. You can define your field separator to anything you like.

    This, however, might be a bit slow since it will reparse $0 and split it into fields, every time you reassign a field $i.

    A faster way is the solution of Dennis Williamson

提交回复
热议问题