awk 상호 참조 2 파일

awk 상호 참조 2 파일

2개의 파일이 있는데 둘 다 2개의 열이 있습니다. 2.txt에 동일한 열이 있으면 1.txt에서 행을 삭제하고 싶습니다.

예 -

1.txt(포함)

example:test
example2:test2
example3:test3

2.txt에는 다음이 포함됩니다(포함)

example:example
example2:example
example3:example
example4:example
example5:example

따라서 1.txt의 열 1에 있는 항목은 2.txt와 동일하므로 이 경우 예상되는 출력은 다음과 같습니다.

example4:example
example5:example

삭제된 콘텐츠 -

example:test
example2:test2
example3:test3

답변1

문제 설명이 더 명확할 수 있지만 예제에 따르면 다음을 원한다고 생각합니다.

awk -F: 'NR==FNR { d[$1]=1; next } !($1 in d) { print $0 }' 1.txt 2.txt

-F:매개변수는 "콜론 문자 ':'를 열 구분 기호로 처리"한다는 의미입니다. (기본적으로 awk연속 공백은 열 구분 기호로 처리됩니다.)

두 번째 매개변수는 awk다음과 같은 프로그램 입니다.

IF this line is from the first input file THEN
    In a dictionary named `d`, create an item whose key is the first column of the input line and whose value is 1
    Skip the rest of the program move on to process the next line

(because of the "skip to next line" above we only do this for lines from the second file)
IF the dictionary named `d` has no item whose whose key is the first column of this line THEN
    Print this line

답변2

다음 명령을 사용하면 잘 작동합니다

awk 'NR==FNR {a[$0];next}($0 in a) {print $0}' 1.txt 2.txt

관련 정보