code

독특한 라인 찾기

codestyles 2020. 11. 8. 10:01
반응형

독특한 라인 찾기


고유 한 줄을 찾고 파일에서 모든 중복을 제거하려면 어떻게해야합니까? 내 입력 파일은

1
1
2
3
5
5
7
7

결과는 다음과 같습니다.

2
3

sort file | uniq일을하지 않을 것입니다. 모든 값을 한 번 표시합니다.


uniq 필요한 옵션이 있습니다.

   -u, --unique
          only print unique lines
$ cat file.txt
1
1
2
3
5
5
7
7
$ uniq -u file.txt
2
3

다음과 같이 사용하십시오.

sort < filea | uniq > fileb

uniq -u는 작동하지 않았기 때문에 나를 미치게 만들었습니다.

그래서 그 대신 파이썬이 있다면 (대부분의 Linux 배포판과 서버에는 이미 있습니다) :

notUnique.txt에 데이터 파일이 있다고 가정합니다.

#Python
#Assuming file has data on different lines
#Otherwise fix split() accordingly.

uniqueData = []
fileData = open('notUnique.txt').read().split('\n')

for i in fileData:
  if i.strip()!='':
    uniqueData.append(i)

print uniqueData

###Another option (less keystrokes):
set(open('notUnique.txt').read().split('\n'))

빈 줄로 인해 최종 집합에 ''또는 공백 만 포함 된 문자열이 포함될 수 있습니다. 나중에 제거 할 수 있습니다. 또는 터미널에서 복사하는 것만으로도 충분하지 않습니다.)

#

참고로 uniq Man 페이지에서 :

"참고 : 'uniq'는 인접하지 않는 한 반복되는 줄을 감지하지 않습니다. 입력을 먼저 정렬하거나 'uniq'없이 'sort -u'를 사용할 수 있습니다. 또한 비교는 'LC_COLLATE'에 지정된 규칙을 따릅니다."

다음으로 호출하는 올바른 방법 중 하나 : # sort nonUnique.txt | 유니크

실행 예 :

$ cat x
3
1
2
2
2
3
1
3

$ uniq x
3
1
2
3
1
3

$ uniq -u x
3
1
3
1
3

$ sort x | uniq
1
2
3

공백이 인쇄 될 수 있으므로 준비하십시오!


당신은 또한 사용 "파일"의 고유 한 값을 출력 할 수 있습니다 cat에 파이프하여 명령을 sort하고uniq

cat file | sort | uniq -u


uniq -u < file 일을 할 것입니다.


uniq어떤 이유로 파일을 정렬 할 수없는 경우 다음을 사용할 수 있습니다 awk.

awk '{a[$0]++}END{for(i in a)if(a[i]<2)print i}'


동안 sort(N (n)이 로그) 시간이 O 소요, 내가 사용하는 것을 선호

awk '!seen[$0]++'

awk '!seen[$0]++'awk '!seen[$0]++ {print}', print line (= $ 0) 의 약어입니다 seen[$0]. 더 많은 공간이 필요하지만 O (n) 시간 만 걸립니다.


sort -d "file name" | uniq -u

this worked for me for a similar one. Use this if it is not arranged. You can remove sort if it is arranged


you can use:

sort data.txt| uniq -u

this sort data and filter by unique values


This was the first i tried

skilla:~# uniq -u all.sorted  

76679787
76679787 
76794979
76794979 
76869286
76869286 
......

After doing a cat -e all.sorted

skilla:~# cat -e all.sorted 
$
76679787$
76679787 $
76701427$
76701427$
76794979$
76794979 $
76869286$
76869286 $

Every second line has a trailing space :( After removing all trailing spaces it worked!

thank you

참고URL : https://stackoverflow.com/questions/13778273/find-unique-lines

반응형