填充空间/制表符分隔,空列0 [英] Filling space/tab separated, empty columns with 0

查看:191
本文介绍了填充空间/制表符分隔,空列0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个巨大的文件,并作为输出某些列不具有值,我需要用0至填充这些列用于进一步分析。我可以用空格或选项卡中的列分开,现在下面可以看出与制表符分隔。

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 tab, now below it is seen separated with tab.

推荐答案

这真是一个CSV解析器的工作,但如果它必须是一个正则表达式,你从来没有引述CSV条目中的选项卡,您可以搜索

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|$)

并将其替换

$10

所以,在Perl:

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;

这将改变

1   2       4           7   8
    2   3       5   6   7   

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

这篇关于填充空间/制表符分隔,空列0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆