我该如何使用SED在CSV文件来替换空白字段,用绳子 [英] how can I use sed to replace blank fields in a csv file, with a string

查看:723
本文介绍了我该如何使用SED在CSV文件来替换空白字段,用绳子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理这些CSV文件,一个脚本,我试图使脚本替换所有空白字段在CSV文件中,有一个字符串DATA_N / A。起初我还以为我可以简单地使用以下方法来完成这项任务:

  SED -eS /高/ DATA_N \\ / A / G

但不幸的是这将离开了那些可能发生在开头或CSV文件的行尾的任何空字段。我不知道如何去这一点,所以我在想,如果有人可以帮助我推在正确的方向或提供意见,以我应该怎么办呢?谢谢!

也在这里是CSV文件的样本:

  ,, 6004 ,,, 15:00.00,30004,科琳,2010-02-10
2,已关闭,6005 ,,, 30:00.00,30005,丰富,2010-02-11
7,关闭,6001 ,,, 30:00.00,10001,玛丽·贝丝·,2010-02-11


解决方案

使用的 perl的正则表达式,

 的perl -pe的; ^,|,$ |(小于?=,),; DATA_N \\ / A; G'input.cvs

使用的 SED

  $ SED -r的; ^,|,$; DATA_N \\ /一,克
:L
S; ,,;,DATA_N \\ /一,克
T L'input.cvs

I am currently working on a script which processes csv files, and I am trying to make the script replace all blank fields in the csv file, with a string "data_n/a". Initially I had thought I could simply use the following to accomplish this task:

sed -e "s/hi/data_n\/a/g"

but unfortunately that would leave out any empty fields that could possibly occur at the beginning or the end of the lines of the csv file. I am not sure how to go about this, so I was wondering if anyone could help push me in the right direction or offer advice as to what I should do? thanks!

also here is a sample of the csv file:

,,6004,,,15:00.00,30004,Colleen,2010-02-10
2,Closed,6005,,,30:00.00,30005,Rich,2010-02-11
7,Closed,6001,,,30:00.00,10001,Mary Beth,2010-02-11

解决方案

Using perl regex,

   perl -pe 's;^,|,$|(?<=,),;data_n\/a,;g' input.cvs

Using sed,

$ sed -r 's;^,|,$;data_n\/a,;g
:l
s;,,;,data_n\/a,;g
t l' input.cvs

这篇关于我该如何使用SED在CSV文件来替换空白字段,用绳子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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