csv에서 날짜 형식 변경

csv에서 날짜 형식 변경

나는 다음 CSV를 가지고 있습니다

9999999,012021,I,0099999999,000000000099999999+,000000000000000000-,000000000000000000-
9999999,012021,I,0099999999,000000000099999999+,000000000000000000-,000000000000000000-
9999999,012021,I,0099999999,000000000099999999+,000000000000000000-,000000000000000000-
9999999,012021,I,0099999999,000000000099999999+,000000000000000000-,000000000000000000-

항상 01일을 날짜로 추가하도록 두 번째 열의 형식을 지정하고 싶습니다.

9999999,2021-01-01,I,0099999999,000000000099999999+,000000000000000000-,000000000000000000-
9999999,2021-01-01,I,0099999999,000000000099999999+,000000000000000000-,000000000000000000-

답변1

다음과 같은 작업이 awk수행됩니다.

awk -F, 'BEGIN {OFS=","} {a=substr($2,1,2);b=substr($2,3,4);$2=b"-"a"-01";print $0}' input_file

이것은 거의 모든 awk.for Solaris 사용 에서 작동합니다. /usr/xpg4/bin/awk또는 /usr/xpg6/bin/awk(@EdMorton의 의견 참조)

PS 나는 012021 012021과 연도 라고 가정합니다.

답변2

사용sed

$ sed 's/\([^,]*,\)\([0-9]\{2\}\)\([^,]*\)\(.*\)/\1\3-\2-01\4/' input_file
9999999,2021-01-01,I,0099999999,000000000099999999+,000000000000000000-,000000000000000000-
9999999,2021-01-01,I,0099999999,000000000099999999+,000000000000000000-,000000000000000000-
9999999,2021-01-01,I,0099999999,000000000099999999+,000000000000000000-,000000000000000000-
9999999,2021-01-01,I,0099999999,000000000099999999+,000000000000000000-,000000000000000000-

관련 정보