填充空格/制表符分隔,空列为0

ber*_*kay 2 regex perl awk sed file

我有一个巨大的文件,作为输出,一些列没有值,我需要用0填充这些列进行进一步分析.我可以用空格或制表符分隔列,现在在下面用制表符分隔.

替代文字

Tim*_*ker 6

这实际上是CSV解析器的工作,但如果它必须是正则表达式,并且您从未在引用的CSV条目中有选项卡,则可以搜索

(^|\t)(?=\t|$)
Run Code Online (Sandbox Code Playgroud)

并替换为

$10
Run Code Online (Sandbox Code Playgroud)

所以,在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;
Run Code Online (Sandbox Code Playgroud)

这将改变

1   2       4           7   8
    2   3       5   6   7   
Run Code Online (Sandbox Code Playgroud)

1   2   0   4   0   0   7   8   
0   2   3   0   5   6   7   0
Run Code Online (Sandbox Code Playgroud)


arn*_*olt 6

对于制表符分隔的文件,此AWK代码段可以解决这个问题:

BEGIN { FS = "\t"; OFS="\t" }
{
    for(i = 1; i <= NF; i++) {
         if(!$i) { $i = 0 }
    }
    print $0
}
Run Code Online (Sandbox Code Playgroud)