![file1의 첫 번째 열에 있는 문자열이 file2에 있는지 확인하는 방법은 무엇입니까?](https://linux55.com/image/140216/file1%EC%9D%98%20%EC%B2%AB%20%EB%B2%88%EC%A7%B8%20%EC%97%B4%EC%97%90%20%EC%9E%88%EB%8A%94%20%EB%AC%B8%EC%9E%90%EC%97%B4%EC%9D%B4%20file2%EC%97%90%20%EC%9E%88%EB%8A%94%EC%A7%80%20%ED%99%95%EC%9D%B8%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F.png)
쉼표로 구분된 문자열이 포함된 두 개의 파일이 있습니다 file1.txt
.
1.1.1.1,string1,comment1
7.7.7.7,string3,comment3
2.2.2.2,string2,comment2
88.88.88.88,string4,comment4
999.999,999,999,string5,comment5
두 번째 파일에는 file2.txt
첫 번째 열에 나타날 수 있는 문자열이 포함되어 있습니다 file1.txt
. file1.txt
첫 번째 열의 문자열이 에 나타나면 전체 행을 삭제해야 합니다 file2.txt
. 원본 파일을 변경하고 싶지는 않지만 출력을 새 파일에 넣고 싶습니다.
답변1
왜 간단하지 않은가?
grep -vffile2 file1
-f FILE: Obtain patterns from FILE, one per line. -v: Invert the sense of matching, to select non-matching lines.
답변2
다음과 같은 것을 시도해 볼 수 있습니다.
#!/bin/bash
cat file2.txt | while IFS=, read line; do
sed -i "/$(grep $line file1.txt)/d" file1.txt
done
이렇게 하면 file1.txt가 직접 변경되지만 원본 파일의 백업 복사본을 저장 sed -i
하도록 명령을 변경할 수 있습니다 .sed -i.ibk
예를 들어
$cat file2.txt
1.1.1.1
7.7.7.7
$cat file1.txt
1.1.1.1,string1,comment1
7.7.7.7,string3,comment3
2.2.2.2,string2,comment2
88.88.88.88,string4,comment4
999.999,999,999,string5,comment5
output
2.2.2.2,string2,comment2
88.88.88.88,string4,comment4
999.999,999,999,string5,comment5