
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
나는 교체하고 싶다
- 파일 의 첫 번째 행에 대한
07822000656
첫 번째 파일(data.csv
) 의 52번째 열 값( )6228205
data.csv
- 파일 의 두 번째 행에 대한 52번째 열 값(
07822000656
)6225214
data.csv
- 52번째 열 값(
07822000656
)은6225211
3번째 행에 해당함
...등등...
그래서 출력은
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는 후행 줄바꿈을 추가하므로 줄바꿈을 추가하세요.