Programing

유닉스에서 파일을 정렬하지 않고 파일에서 중복 줄을 삭제하는 방법은 무엇입니까?

crosscheck 2020. 7. 19. 10:25

유닉스에서 파일을 정렬하지 않고 파일에서 중복 줄을 삭제하는 방법은 무엇입니까?


유닉스 파일에서 중복 줄을 삭제하는 방법이 있습니까?

sort -uuniq명령으로 할 수 있지만 sed또는 을 사용하고 싶습니다 awk. 가능합니까?


awk '!seen[$0]++' file.txt

seenAwk가 파일의 모든 줄을 전달할 연관 배열입니다. 행이 배열에 없으면 seen[$0]false로 평가됩니다. !논리적 NOT 연산자 true로 거짓을 반전합니다. Awk는 표현식이 true로 평가되는 행을 인쇄합니다. ++증가 seen되도록 seen[$0] == 1제 시간 후에 라인하고 발견 seen[$0] == 2등.
Awk는 0""(빈 문자열)을 제외한 모든 것을 평가 합니다. 중복 라인에 배치되어있는 경우 seen다음 !seen[$0]false로 평가되고 라인은 출력에 기록되지 않습니다.


에서 http://sed.sourceforge.net/sed1line.txt : (어떻게이 일을 부탁하지 마십시오 ;-))

 # delete duplicate, consecutive lines from a file (emulates "uniq").
 # First line in a set of duplicate lines is kept, rest are deleted.
 sed '$!N; /^\(.*\)\n\1$/!P; D'

 # delete duplicate, nonconsecutive lines from a file. Beware not to
 # overflow the buffer size of the hold space, or else use GNU sed.
 sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'

@jonas의 awk 솔루션과 비슷한 Perl one-liner :

perl -ne 'print if ! $x{$_}++' file

이 변형은 다음을 비교하기 전에 후행 공백을 제거합니다.

perl -lne 's/\s*$//; print if ! $x{$_}++' file

이 변형은 파일을 내부 편집합니다.

perl -i -ne 'print if ! $x{$_}++' file

이 변형은 파일을 내부 편집하고 백업합니다 file.bak

perl -i.bak -ne 'print if ! $x{$_}++' file

Andre Miller가 위에 게시 한 라이너는 입력 파일이 빈 줄로 끝나고 문자가없는 경우 최신 버전의 sed를 제외하고 작동합니다. 내 Mac에서 CPU가 회전합니다.

마지막 줄이 비어 있고 문자가없는 경우 무한 루프 :

sed '$!N; /^\(.*\)\n\1$/!P; D'

멈추지 않지만 마지막 줄을 잃습니다.

sed '$d;N; /^\(.*\)\n\1$/!P; D'

설명은 sed FAQ 의 맨 끝에 있습니다 .

GNU sed 관리자는 이식성 문제에도 불구하고
N 명령을 변경 (
삭제 대신 인쇄)하도록 변경하면
"다음 줄 추가"명령 이 어떻게 동작 해야하는지대한 직관적 인 패턴 공간과 일치 한다고 느꼈습니다 .
변경을 선호하는 또 다른 사실
은 파일에 홀수가있는 경우 "{N; command;}"은 마지막 라인 삭제하지만 파일에 짝수 개의
라인이 있으면 마지막 라인을 인쇄한다는 것입니다.

이전의 N 동작 (
EOF에 도달 할 때 패턴 공간 삭제 )을 사용한 스크립트를
모든 버전의 sed 와 호환 되는 스크립트로 변환하려면 고독한 "N;"을 변경하십시오. "$ d; N;" .


Vim (Vi 호환)을 사용하는 다른 방법 :

파일에서 연속 된 중복 행을 삭제합니다.

vim -esu NONE +'g/\v^(.*)\n\1$/d' +wq

파일에서 연속적이며 비 연속적인 행을 삭제합니다.

vim -esu NONE +'g/\v^(.+)$\_.{-}^\1$/d' +wq


첫 번째 해결책은 또한 http://sed.sourceforge.net/sed1line.txt입니다.

$ echo -e '1\n2\n2\n3\n3\n3\n4\n4\n4\n4\n5' |sed -nr '$!N;/^(.*)\n\1$/!P;D'
1
2
3
4
5

핵심 아이디어는 다음과 같습니다.

print ONLY once of each duplicate consecutive lines at its LAST appearance and use D command to implement LOOP.

설명합니다 :

  1. $!N;: 현재 행이 마지막 행이 아닌 경우 N명령을 사용 하여 다음 행을로 읽어보십시오 pattern space.
  2. /^(.*)\n\1$/!P: 전류의 내용 pattern space이 두 개로 duplicate string분리되어 있으면 \n다음 줄이 same현재 줄과 같다는 것을 우리의 핵심 아이디어에 따라 인쇄 할 수 없습니다. 그렇지 않으면, 현재 행이 모든 중복 연속 행의 마지막 모양임을 의미합니다. 이제 P명령을 사용 하여 현재 pattern spaceutil 에서 문자를 인쇄 할 수도 있습니다 \n( \n또한 인쇄 됨).
  3. D: we use D command to delete the chars in current pattern space util \n (\n also deleted), then the content of pattern space is the next line.
  4. and D command will force sed to jump to its FIRST command $!N, but NOT read the next line from file or standard input stream.

The second solution is easy to understood (from myself):

$ echo -e '1\n2\n2\n3\n3\n3\n4\n4\n4\n4\n5' |sed -nr 'p;:loop;$!N;s/^(.*)\n\1$/\1/;tloop;D'
1
2
3
4
5

the core idea is:

print ONLY once of each duplicate consecutive lines at its FIRST appearance and use : command & t command to implement LOOP.

Explains:

  1. read a new line from input stream or file and print it once.
  2. use :loop command set a label named loop.
  3. use N to read next line into the pattern space.
  4. use s/^(.*)\n\1$/\1/ to delete current line if the next line is same with current line, we use s command to do the delete action.
  5. if the s command is executed successfully, then use tloop command force sed to jump to the label named loop, which will do the same loop to the next lines util there are no duplicate consecutive lines of the line which is latest printed; otherwise, use D command to delete the line which is the same with thelatest-printed line, and force sed to jump to first command, which is the p command, the content of current pattern space is the next new line.

This can be achieved using awk
Below Line will display unique Values

awk file_name | uniq

You can output these unique values to a new file

awk file_name | uniq > uniq_file_name

new file uniq_file_name will contain only Unique values, no duplicates


cat filename | sort | uniq -c | awk -F" " '$1<2 {print $2}'

Deletes the duplicate lines using awk.

참고URL : https://stackoverflow.com/questions/1444406/how-to-delete-duplicate-lines-in-a-file-without-sorting-it-in-unix