IT

텍스트 파일에서 Linux 명령 또는 확장에서 확장 행을 계산합니까?

lottoking 2020. 8. 5. 07:40
반응형

텍스트 파일에서 Linux 명령 또는 확장에서 확장 행을 계산합니까?


다음 내용이 포함 된 텍스트 파일이있는 경우

red apple
green apple
green apple
orange
orange
orange

다음 결과를 얻는 데 사용할 수있는 Linux 명령 또는 운영이 있습니까?

1 red apple
2 green apple
3 orange

그것을 통해 sort(인접한 항목을 모으기 위해) 보낸 다음 수 uniq -c를 세십시오.

sort filename | uniq -c

목록을 빈도별로 정렬 된 순서대로 가져 오려면

sort filename | uniq -c | sort -nr

borribles 거의 동일하지만와 d매개 변수를 추가하면 uniq중복 항목 만-display됩니다.

sort filename | uniq -cd | sort -nr

uniq -c file

파일이 아직 정렬되지 않은 경우 :

sort file | uniq -c


이 시도

cat myfile.txt| sort| uniq

cat <filename> | sort | uniq -c

알파벳순으로 정렬 된 목록으로 살 수 있습니까?

echo "red apple
> green apple
> green apple
> orange
> orange
> orange
> " | sort -u 

?

green apple
orange
red apple

또는

sort -u FILE

-u는 고유성을 사용하여 고유성은 이용할 수 있습니다.

순서를 유지하는 솔루션 :

echo "red apple
green apple
green apple
orange
orange
orange
" | { old=""; while read line ; do   if [[ $line != $old ]]; then  echo $line;   old=$line; fi ; done }
red apple
green apple
orange

그리고 파일과 함께

cat file | { 
old=""
while read line
do
  if [[ $line != $old ]]
  then
    echo $line
    old=$line
  fi
done }

마지막 두 개는 감시 만 제거하고 즉시 따라갑니다-귀하의 예에 맞습니다.

echo "red apple
green apple
lila banana
green apple
" ...

두 개의 사과를 바나나로 나눠서 인쇄합니다.


얻을 수려면 :

$> egrep -o '\w+' fruits.txt | sort | uniq -c

      3 apple
      2 green
      1 oragen
      2 orange
      1 red

정렬 된 개수를 얻으려면 :

$> egrep -o '\w+' fruits.txt | sort | uniq -c | sort -nk1
      1 oragen
      1 red
      2 green
      2 orange
      3 apple

편집하다

아하, 이것은 단어 경계를 따르는 것이 아닙니다. 전체 줄에 사용할 명령은 다음과 같습니다.

$> cat fruits.txt | sort | uniq -c | sort -nk1
      1 oragen
      1 red apple
      2 green apple
      2 orange

참고 URL : https://stackoverflow.com/questions/6447473/linux-command-or-script-counting-duplicated-lines-in-a-text-file

반응형