且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我该如何使用SED在CSV文件来替换空白字段,用绳子

更新时间:2023-11-18 10:19:46

使用的 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