remove a entrada duplicada do arquivo1 se a entrada estiver presente no arquivo2

0

Eu quero excluir a entrada duplicada e do file1 se e também estiver presente em file2 .

Entrada file1 :

x1  y1
x2  y2
x3  y3
x4  y4
y1  x1
x5  y5
y3  x3
x6  y6
x5  y5

Entrada file2 :

y1  x1
y2  x2
y3  x3
y4  x4
x1  y1
y5  x5
x3  y3
y6  x6
x5  y5

Saída desejada:

x1  y1
x2  y2
x3  y3
x4  y4
x5  y5
x6  y6

Eu usei o seguinte script de shell:

awk 'FNR==NR {

   lines[NR,"col1"] = $1
   lines[NR,"col2"] = $2
   lines[NR,"line"] = $0
   next
    }

  (lines[FNR,"col1"] != $1) {($1 in lines)
    print lines[FNR,"line"]
    next
}' file1.txt file2.txt

Mas está dando a seguinte saída:

x1  y1
x2  y2
x3  y3
x4  y4
y1  x1
x5  y5
y3  x3
x6  y6
    
por shachi 31.07.2014 / 12:55

2 respostas

2

Primeiro: sua saída desejada deve ser:

y2  x2
y4  x4
y5  x5
y6  x6

porque "x3 y3" e "x1 y1" estão presentes nos dois arquivos

para obter linhas que estão presentes no arquivo 1 só você pode simplesmente fazer

grep -v -f file1 file2

Da página man

-v
--invert-match
 Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.)

-f file
   --file=file
Obtain patterns from file, one per line. The empty file contains zero patterns, and therefore matches nothing. (-f is specified by POSIX.)  
    
por 31.07.2014 / 16:32
0

Tente isto:

awk '{if($1>$2) print $2 "  " $1; else print $0;}' file1.txt file2.txt | sort -u > out.txt

esta saída:

x1  y1
x2  y2
x3  y3
x4  y4
x5  y5
x6  y6

awk simplesmente reordena colunas por ordem alfabética, sort -u (exclusivo) remove linhas duplicadas.

    
por 02.08.2014 / 00:45

Tags