file1의 첫 번째 열에 있는 문자열이 file2에 있는지 확인하는 방법은 무엇입니까?

file1의 첫 번째 열에 있는 문자열이 file2에 있는지 확인하는 방법은 무엇입니까?

쉼표로 구분된 문자열이 포함된 두 개의 파일이 있습니다 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

관련 정보