Awk
包含不干擾 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-,
使用
substr()
:awk -F, '$5 = substr($5,1,length($5)-3) "." substr($5,length($5)-2)' OFS=,
如果不強制使用 awk ,則使用Miller,如果您希望逗號作為小數分隔符
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-,