반응형
한 줄에 하나의 항목이있는 두 텍스트 파일의 차이점 찾기
이 질문에 이미 답변이 있습니다.
두 개의 파일이 있습니다.
파일 1
dsf
sdfsd
dsfsdf
파일 2
ljljlj
lkklk
dsf
sdfsd
dsfsdf
파일 1이 아닌 파일 2에있는 내용을 표시하고 싶으므로 파일 3은 다음과 같아야합니다.
ljljlj
lkklk
당신은 시도 할 수 있습니다
grep -f file1 file2
또는
grep -v -F -x -f file1 file2
grep -Fxvf file1 file2
플래그의 의미 :
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.
-x, --line-regexp
Select only those matches that exactly match the whole line.
-v, --invert-match
Invert the sense of matching, to select non-matching lines.
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing.
comm
명령을 사용하여 두 개의 정렬 된 파일을 비교할 수 있습니다.
comm -13 <(sort file1) <(sort file2)
나는 성공적으로 사용했다
diff "${file1}" "${file2}" | grep "<" | sed 's/^<//g' > "${diff_file}"
차이를 파일로 출력합니다.
특정 순서로 예상하는 경우 diff
diff file1 file2 | grep ">"
join -v 2 <(sort file1) <(sort file2)
A는 Luca 의 대답 에 약간의 변형을 시도 했고 그것은 나를 위해 일했습니다.
diff file1 file2 | grep ">" | sed 's/^> //g' > diff_file
sed에서 검색된 패턴은 >
뒤에 공백이 있습니다.
file1 m1 m2 m3 file2 m2 m4 m5 > awk 'NR == FNR {file1 [$ 0] ++; next}! ($ 0 in file1) 'file1 file2 m4 m5 > awk 'NR == FNR {file1 [$ 0] ++; next} ($ 0 in file1) 'file1 file2 m2 > 'm1 및 m3'을 가져 오는 awk 명령은 무엇입니까? file2가 아니라 file1에서와 같이? m1 m3
루프를 사용하려면 다음과 같이 시도 할 수 있습니다. (diff 및 cmp가 훨씬 더 효율적입니다.)
while read line
do
flag = 0
while read line2
do
if ( "$line" = "$line2" )
then
flag = 1
fi
done < file1
if ( flag -eq 0 )
then
echo $line > file3
fi
done < file2
참고 :이 프로그램은 diff n comm과 같은 시스템 호출을 사용하지 않으려는 경우 수행 할 수있는 작업에 대한 기본적인 통찰력을 제공하기위한 것입니다 ..
awk 대답 :
awk 'NR == FNR {file1[$0]++; next} !($0 in file1)' file1 file2
GNU 사용 sed
:
sed 's#[^^]#[&]#g;s#\^#\\^#g;s#^#/^#;s#$#$/d#' file1 | sed -f- file2
작동 원리 :
첫 번째 sed
는 다음과 같은 출력을 생성합니다.
/^[d][s][f]$/d
/^[s][d][f][s][d]$/d
/^[d][s][f][s][d][f]$/d
그런 다음 sed
두 번째 스크립트로 사용됩니다 sed
.
반응형
'development' 카테고리의 다른 글
파이썬 : 소수점 1 자리 만 얻기 (0) | 2020.10.16 |
---|---|
ActionBar 안에 아이콘과 작업 제목을 모두 표시하는 방법은 무엇입니까? (0) | 2020.10.16 |
동적이지만 정사각형 레이아웃을 수행하는 간단한 방법 (0) | 2020.10.15 |
UILabel의 textColor 속성에 애니메이션을 적용하는 방법은 무엇입니까? (0) | 2020.10.15 |
지연된 함수 호출 (0) | 2020.10.15 |