Filling space/tab separated, empty columns with 0

前端 未结 5 2059
天命终不由人
天命终不由人 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:28

    This is really a job for a CSV parser, but if it has to be a regex, and you never have tabs within quoted CSV entries, you could search for

    (^|\t)(?=\t|$)
    

    and replace with

    $10
    

    So, in Perl:

    (ResultString = $subject) =~ 
    s/(    # Match either...
       ^   # the start of the line (preferably)
       |   # or
       \t  # a tab character
      )    # remember the match in backreference no. 1
      (?=  # Then assert that the next character is either
       \t  # a(nother) tab character
       |   # or
       $   # the end of the line
      )    # End of lookahead assertion
    /${1}0/xg;
    

    This will transform

    1   2       4           7   8
        2   3       5   6   7   
    

    into

    1   2   0   4   0   0   7   8   
    0   2   3   0   5   6   7   0
    

提交回复
热议问题