소팅된 리스팅 2개 파일에서 원하는 내용만 추출하는 방법

choco6의 이미지

가령, 다음과 같은 2개의 파일이 있습니다.
file1.txt

----------------------------------
보장번호              사용일
----------------------------------
0000100225   2008-04-02 09:30:26
0000101193   2008-04-02 09:35:46
0000101545   2008-04-02 09:35:46
0000101803   2008-04-02 09:35:46
0000103215   2008-04-02 09:35:46
0000112305   2008-04-02 09:35:46
 ...
<중략>

file2.txt

----------------------------------
보장번호              사용일
----------------------------------
0000101193   2008-04-02 09:50:27
0000101545   2008-04-02 09:50:27
0000101803   2008-04-02 09:50:27
0000103215   2008-04-02 09:50:27
0000112305   2008-04-02 09:50:27
0000114725   2008-04-02 09:50:27
...
<중략>

여기서 첫번째 필드인 보장번호를 키로 삼아서 file1.txt에는 존재하지만 file2.txt에는 존재하지 않는 번호만 추출해서 화면에 출력시키고자 합니다.. 화면에 출력시킬 때 보장번호뿐 아니라 사용일도 함께 출력시키고 싶구요..
이렇게 하려면 c언어로 프로그래밍을 짜면 되겠지만, sed나 awk등을 이용해 쉘로도 간단히 작성할 수 있을 것 같은데 잘 안떠오르네요..
혹시 이런 요구사항에 대하여 쉘로도 작성 가능할런지요?
고수분의 조언 부탁합니다.

오호라의 이미지

뭔가 있을거 같은 단어인데요. ^^;

#!/bin/bash
 
keylist=`tail -n +4 file1.txt | cut -c -10`
 
for key in $keylist ;
do
    grep -R "$key" file2.txt
done

속도(양)이 문제라면 정렬된 키를 이용하면 O(n) 로 끝나겠죠. ^^

주의할점. 요즘 배포판은 그닥... ^^;

>info tail

Quote:
On older systems, `tail' supports an obsolete option
`-COUNTOPTIONS', which is recognized only if it is specified first.
COUNT is a decimal number optionally followed by a size letter (`b',
`k', `m') as in `-c', or `l' to mean count by lines, or other option
letters (`cfqv'). Some older `tail' implementations also support an
obsolete option `+COUNT' with the same meaning as `-+COUNT'. POSIX
1003.1-2001 (*note Standards conformance::) does not allow these
options; use `-c COUNT' or `-n COUNT' instead.

Hello World.

alfalf의 이미지

Bash 환경이라면 아래와 같이 하시면 됩니다.

fgrep -v -f <(cut -f1 file2.txt) file1.txt

grep을 사용하기 때문에 정렬이 안됐어도 사용할 수 있습니다.