包含一个区分分号的字符,不会干扰 csv 文件的分隔符

包含一个区分分号的字符,不会干扰 csv 文件的分隔符

我有一个 CSV 文件,需要包含一个分隔符以突出显示该列中不干扰“,”分隔符的分

下面是我当前文件的模型:

99999999,2021-01-01,I,0090922002,000000000009102629-,000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0091000002,000000000063288833+,000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0091100005,000000000063288833-,000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0091110002,000000000063288833-,000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0099999995,000000008017897139+,000000000000000000-,000000000000000000-,

由于文件分隔符是“,”,我相信“.”使用起来很酷。我怎样才能像这样保留文件?

99999999,2021-01-01,I,0090922002,0000000000091026.29-,000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0091000002,0000000000632888.33+,000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0091100005,0000000000632888.33-,000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0091110002,0000000000632888.33-,000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0099999995,0000000080178971.39+,000000000000000000-,000000000000000000-,

答案1

如果不强制使用 awk,则使用磨坊主,如果您想要逗号作为小数点分隔符

mlr --csv -N put -S '$5=sub($5,"(...)$",",\1")' input.txt

给你

99999999,2021-01-01,I,0090922002,"0000000000091026,29-",000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0091000002,"0000000000632888,33+",000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0091100005,"0000000000632888,33-",000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0091110002,"0000000000632888,33-",000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0099999995,"0000000080178971,39+",000000000000000000-,000000000000000000-,

如果您想要点作为小数点分隔符

mlr --csv -N put -S '$5=sub($5,"(...)$",".\1")' input.txt
99999999,2021-01-01,I,0090922002,0000000000091026.29-,000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0091000002,0000000000632888.33+,000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0091100005,0000000000632888.33-,000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0091110002,0000000000632888.33-,000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0099999995,0000000080178971.39+,000000000000000000-,000000000000000000-,

答案2

使用substr()

awk -F, '$5 = substr($5,1,length($5)-3) "." substr($5,length($5)-2)' OFS=,

答案3

$ awk 'BEGIN{FS=OFS=","} {sub(/...$/,".&",$5)} 1' file
99999999,2021-01-01,I,0090922002,0000000000091026.29-,000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0091000002,0000000000632888.33+,000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0091100005,0000000000632888.33-,000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0091110002,0000000000632888.33-,000000000000000000-,000000000000000000-,
99999999,2021-01-01,I,0099999995,0000000080178971.39+,000000000000000000-,000000000000000000-,

答案4

如果你不必使用 awk,GoCSV 的代替命令可以处理这个:

gocsv replace -c 5 -regex '(\d{2}[+-])' -repl '.$1' input.csv

实际上,因为您的示例没有标题,所以我们需要添加标题(cap),进行替换,然后删除标题(behead):

gocsv cap --default-name 'Col' input.csv |
gocsv replace -c 5 -regex '(\d{2}[+-])' -repl '.$1' | 
gocsv behead

相关内容