중복된 줄만 출력하려고 함니다..

hyean의 이미지

파일내용이
1 1 1 1 1
2 2 2 2 2
a a a a a
a a a a a
3 3 3 3 3
이런 경우에

출력이
a a a a a
a a a a a
이런식으로 가능한가요??

astronux의 이미지

원하시는대로는 아니겠지만, 'uniq'라는 명령어의 결과를 조금 응용하시면, 되지 않을까요?

# cat test.txt
1 1 1 1
2 2 2 2
a a a a
a a a a
b b b b
c c c c
c c c c
c c c c

중복 및 단일 라인 상관없이 한 줄씩만 출력 : # uniq test.txt
예) # uniq test.txt
1 1 1 1
2 2 2 2
a a a a
b b b b
c c c c

중복되는 라인만 한 줄 출력 : # uniq -d test.txt
예) # uniq -d test.txt
a a a a
c c c c

중복되는 라인이 몇 줄인지 출력 : # uniq -d -c test.txt
예) # uniq -d -c test.txt
2 a a a a
3 c c c c

Astronomy+Linux

Astronomy+Linux

hyean의 이미지

답변감사합니다.. 질문하나만더할께요..

중복되는 라인만 한 줄 출력 : # uniq -d test.txt

여기서여 중복되는 라인만 한 줄 출력이 아닌
중복되는 라인은 전부 출력할 수 있나요??

제가하고싶은것이
111 ab
112 ac
113 aa
113 aa
115 bb
이런텍스트에서 앞에 필드가 같으면 그 줄을 출력하고 싶어서요..
113 aa
113 aa
이런식으로,,

jg의 이미지

uniq -d -c test.txt | awk '{ cnt = $1 + 0; sub(/^ *[0-9]+ /, ""); for(j=0; j < cnt; ++j) print;}'

==

하핫, 불필요한 코드네요. -D 옵션이 있습니다.

$Myoungjin_JEON=@@=qw^rekcaH lreP rehtonA tsuJ^;$|++;{$i=$like=pop@@;unshift@@,$i;$~=18-length$i;print"\r[","~"x abs,(scalar reverse$i),"~"x($~-abs),"]"and select$good,$day,$mate,1/$~for 0..$~,-$~+1..-1;redo}

astronux의 이미지

맨페이지를 찾아보니, '-D' 옵션이 있네요.
아마 이게 원하시는게 아닐까 싶네요.
즉, test.txt의 내용을
111 ab
112 ac
113 aa
113 aa
115 bb
이라고 가정한다면,

# uniq -D test.txt
113 aa
113 aa

라고 나오는군요.

Astronomy+Linux

Astronomy+Linux

hyean의 이미지

아.. 죄송함니다.. 제가문제를 잘못적었습니다;;;
다시한번만 답변해주시면 감사하겠습니다..

111 ab
112 ac
113 aa
113 aa
115 bb
이게 아니라..
111 ab
112 ac
113 aa
113 zz
115 bb

이런텍스트에서 앞에 필드가 같으면(3,4번재줄) 그 줄을 출력하고 싶어서요..
113 aa
113 zz
이런식이엿습니다..

jg의 이미지

우선 awk 로는 이렇게 할 수 있습니다만..

awk '{
        id = $1 + 0;
        ++hits[id];
        if (hits[id] == 1) { first[id] = $0; }
        else {
                if ( hits[id] == 2) print first[id];
                print;
        }
}' test.txt

$Myoungjin_JEON=@@=qw^rekcaH lreP rehtonA tsuJ^;$|++;{$i=$like=pop@@;unshift@@,$i;$~=18-length$i;print"\r[","~"x abs,(scalar reverse$i),"~"x($~-abs),"]"and select$good,$day,$mate,1/$~for 0..$~,-$~+1..-1;redo}

jg의 이미지

grep -vF "`sort  -k1 test.txt | uniq -u test.txt`" test.txt

이렇게 해도 될 것 같군요.

$Myoungjin_JEON=@@=qw^rekcaH lreP rehtonA tsuJ^;$|++;{$i=$like=pop@@;unshift@@,$i;$~=18-length$i;print"\r[","~"x abs,(scalar reverse$i),"~"x($~-abs),"]"and select$good,$day,$mate,1/$~for 0..$~,-$~+1..-1;redo}

hyean의 이미지

jp님 답변 감사함니다~

궁금하면 또 질문할께여 ^^~~