![awk는 다른 파일의 일치하는 값을 기반으로 필드 값을 바꿉니다.](https://linux55.com/image/154733/awk%EB%8A%94%20%EB%8B%A4%EB%A5%B8%20%ED%8C%8C%EC%9D%BC%EC%9D%98%20%EC%9D%BC%EC%B9%98%ED%95%98%EB%8A%94%20%EA%B0%92%EC%9D%84%20%EA%B8%B0%EB%B0%98%EC%9C%BC%EB%A1%9C%20%ED%95%84%EB%93%9C%20%EA%B0%92%EC%9D%84%20%EB%B0%94%EA%BF%89%EB%8B%88%EB%8B%A4..png)
나는 2개의 파일을 가지고 Zipcode.txt
있으며 Address.csv
:
ZipCode.txt
12345
23456
34567
45678
Address.csv
12345,3587 main st,apt j1,city,new jersey
23456,4215 1st st. s.,suite a2,city,new jersey
65432,115 main st,,city,new jersey
45678,654 2nd st n.,city,new jersey
의 우편번호 필드가 Zipcode.txt
의 우편번호 필드와 일치하는 경우 네 번째 필드를 에서 로 Address.csv
변경하고 싶습니다 . 이것이 내가 원하는거야:city
found
12345,3587 main st,apt j1,found,new jersey
23456,4215 1st st. s.,suite a2,found,new jersey
65432,115 main st,,city,new jersey
45678,654 2nd st n.,found,new jersey
내가 시도한 것은 다음과 같습니다.
awk -F',' 'BEGIN{OFS=FS}NR==FNR{a[$1]=1;next}a[$1]{$4="found"}1' Address.csv ZipCode.txt
답변1
Address.csv
다음 위치에서 우편번호가 발견 되면 첫 번째 필드에서 우편번호를 검색합니다 Zipcode.txt
.
awk -F, -v OFS="," 'NR==FNR {a[$1]++;next} $1 in a {$4="found"} 1' Zipcode.txt Address.csv
산출:
12345,3587 main st,apt j1,found,new jersey
23456,4215 1st st. s.,suite a2,found,new jersey
65432,115 main st,,city,new jersey
45678,654 2nd st n.,city,found
마지막 줄은 city
입력의 네 번째 필드가 아니기 때문에 예상한 것과 다릅니다. 마지막 줄에 쉼표가 누락되었을 수 있습니다 Address.csv
.