[스크립트] 연속적으로 중복되는 필드 제거

whited85의 이미지

안녕하세요. (__)

스크립트 초보입니다..

일하다가 단순하게 반복하는 작업이 있는데

양이 너무 많아 스크립트로 처리하려고 했는데요...

중복행 같은경우에는 awk 와 sed를 이용해서 제거가 간단하게 되었는데

중복되는열(필드) 같은경우에는 어떤 방법이 있을까요..?

데이터가 여러행에 있고 각 행마다

연속적으로 중복되는 필드가 있을 경우 유일하게 한개만 남겨두고 제거해야 합니다..

도움 좀 부탁 드리겠습니다.. ㅠㅠ

예제 원본
========================================
13074 13075 13076 13076 13076 13076
15050 15050 15051 15052 15053 15053
12051 12051 13051 15351 15053 15054
========================================
 
원하는결과값
========================================
13074 13075 13076
15050 15051 15052 15053
12051 13051 15351 15053 15054
========================================
백연구원의 이미지

awk '{str="";c=0;split($0,arr," "); for (v in arr) c++; for (m=c;m >= 1;m--) for (n=1; n<m;n++) if (arr[m] == arr[n]) delete arr[m]; for (k=1;k<=c;k++) {if (k ==0 ) {s=arr[k] } else if (arr[k] != "") str=str" "arr[k] } print str}'

[root@localhost oops]# awk '{str="";c=0;split($0,arr," "); for (v in arr) c++; for (m=c;m >= 1;m--) for (n=1; n<m;n++) if (arr[m] == arr[n]) delete arr[m]; for (k=1;k<=c;k++) {if (k ==0 ) {s=arr[k] } else if (arr[k] != "") str=str" "arr[k] } print str}' a.txt
 13074 13075 13076
 15050 15051 15052 15053
 12051 13051 15351 15053 15054
[root@localhost oops]# 

출처 : http://www.folkstalk.com/2012/09/remove-duplicate-strings-words-from.html

위에 내용 응용하시면 되겠습니다..


소곤소곤

whited85의 이미지

답변 감사 드립니다. (__)

bushi의 이미지

$ cat x.txt 
13074 13075 13076 13076 13076 13076
15050 15050 15051 15052 15053 15053
12051 12051 13051 15351 15053 15054
 
$ while read line; do echo `echo -en "${line// /\\n}\n" | uniq`; done < x.txt 
13074 13075 13076
15050 15051 15052 15053
12051 13051 15351 15053 15054

"연속으로 중복된 열" 만 제거됩니다. 중복이지만 연속이지 않은 열은 살아남습니다.
익명 사용자의 이미지

와우. 이거 멋지지네요. +++++1

whited85의 이미지

정말 딱 제가 원하던 거네요..
이렇게 간단하게 만들어내시다니.. 생각도 못했네요 uniq 쓸 생각만 했지.. 응용을 못했네요
감사합니다.

shint의 이미지

1시간 걸렸습니다. ㅡ_ㅡ;;

http://xmkaelx.blog.me/40066078000

http://kldp.org/node/153762

http://codepad.org/

#!/usr/bin/perl
 
use strict; use warnings;
 
my $val1 = "13074 13075 13076 13076 13076 13076";
my $val2 = "15050 15050 15051 15052 15053 15053";
my $val3 = "12051 12051 13051 15351 15053 15054";
 
my @list;
push(@list, $val1);
push(@list, $val2);
push(@list, $val3);
my $count;
$count = scalar @list;
print("push 된 총갯수: $count 개\n");
 
for(my $i=0; $i<$count; $i++)
{
    my @ar = split(' ', $list[$i]);    #pop(@list); pop()은 순서가 거꾸로 나온다.
 
    @ar = uniqueElements(@ar);
    print "$_ " foreach (@ar);
    print "\n";
}
 
 
 
my @T1;
$T1[0] = $val1;
$T1[1] = $val2;
$T1[2] = $val3;
$count = scalar @T1;
print("T1 총갯수: $count 개\n");
 
 
for(my $i=0; $i<$count; $i++)
{
    my @ar = split(' ', $T1[$i]);    #pop(@list); pop()은 순서가 거꾸로 나온다.
 
    @ar = uniqueElements(@ar);
    print "$_ " foreach (@ar);
    print "\n";
}
 
 
 
sub uniqueElements
{
    my($item, %seen, @result);
 
    foreach $item (@_)
    {
        push(@result, $item) unless $seen{$item}++;
    }
    return @result;
}

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

whited85의 이미지

너무 감사합니다! 능력자분들.. 1시간 ㅠㅠ

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.