"," 구분 기호를 방해하지 않고 열의 구분선을 강조 표시하기 위해 구분 기호를 포함해야 하는 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)을 추가하고 대체한 다음 캡(참수)을 제거해야 합니다.
gocsv cap --default-name 'Col' input.csv |
gocsv replace -c 5 -regex '(\d{2}[+-])' -repl '.$1' |
gocsv behead