csv 파일의 열 값을 다른 파일에서 교체

csv 파일의 열 값을 다른 파일에서 교체

85개 필드가 있는 CSV 파일이 있습니다. 열 52의 값을 다른 파일의 데이터로 바꾸고 싶습니다. 두 번째 파일에는 열이 1개만 포함되어 있으며 첫 번째 파일과 동일한 수의 레코드가 있습니다.

예시 data.CSV파일(첫 번째 csv 파일)

0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111937,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**07822000656**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0,
0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111938,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**07822000656**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0,
0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111939,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**07822000656**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0,
0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111939,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**07822000656**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0,
0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111939,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**07822000656**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0,
0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111939,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**07822000656**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0,

두 번째 파일(열 1개만 포함)

6228205
6225214
6225211
6225206
5206
87777

나는 바꾸고 싶다

  • 52열 값( 07822000656)은 첫 번째 파일( data.csv)과 파일 6228205의 첫 번째 줄 에서 나옵니다.data.csv
  • 파일에서 행 2의 열 52 값( 07822000656)6225214data.csv
  • 52열 값( 07822000656) 및 62252113행

...등...

그래서 출력은 다음과 같아야합니다

0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111937,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**6228205**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0,
0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111938,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**6225214**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0,
0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111939,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**6225211**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0,
0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111939,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**6225206**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0,
0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111939,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**5206**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0,
0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111939,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**87777**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0,

나는 다음과 같이 그것을 할 수 있었다 :

awk -F , '{$1, $2, $3, $4...$51}' data.csv >temp1.csv
awk -F , '{$53, $54, $55....$85}' data.csv >temp2.csv
paste -d "," temp1.csv 2nd_file temp2.csv

그러나 나는 이 문제를 처리할 더 나은 방법을 찾고 있습니다.

답변1

두 번째 파일에 항목 맵을 만들고 awk, 줄 번호를 입력하고, 다음을 사용하여 첫 번째 파일의 값을 바꿀 수 있습니다.

awk -v FS=, -v OFS=, 'FNR==NR{hash[FNR]=$0; next}{$52 = hash[FNR]}1' file2 file1

답변2

이 질문/awk에 태그를 지정했지만 awk유사한 필드가 있는 csv 파일이 있으면 유사한 도구를 사용하면 실패하므로 "embed , in a string"이를 위해 설계된 도구를 사용하는 것이 더 낫습니다. 이는 매우 간단합니다.

csvtool pastecol 52 1 data.CSV value.CSV

그러면 열 52가 data.CSV열 1로 대체됩니다.value.CSV

답변3

사용 방법은 다음과 같습니다.GoCSV, CSV 처리를 위해 설계된 도구입니다.

# Break up starting-file about column 52
gocsv select --columns 1-51 start.csv > left.csv
gocsv select --columns 53-  start.csv > right.csv

# Combine both sides with replacement column/file in the "middle"
gocsv zip left.csv replacement.csv right.csv > my_final.csv

# Prove it worked
cmp my_final.csv op_final.csv 

나는 옷을 입어야 해요.op_final.csv비교하시기 전, 한번 해보고 싶으신 분들을 위해

  • **목표값 주변 OP 제거
  • GoCSV가 후행 줄바꿈을 추가하므로 줄바꿈 추가

관련 정보