search multiple patterns

I have two lists in a file that look like
a b
b a
e f
c d
f e
d c

I would like a final list
a b
c d
e f

I've tried multiple grep and awk but can't get it to work

grep can't do it because grep recalls nothing about previous lines. Even in awk you have to tell it to remember, which I do with the associative array A.

$ echo "a b
b a
e f
c d
f e
d c
" | awk '{
        # get a b order even if we get b a
        if($1 > $2) { tmp=$1; $1=$2; $2=tmp; }
        if(A[$1] != $2)  { print ; A[$1]=$2; }
}' | sort # sort is necessary for your example data, otherwise we get a b \n e f \n ...
a b
c d
e f
$
1 Like

Try this awk script which sorts each line and removes the dups...

awk '{
   n = split($0,a," ")
   for (i=1; i<n; i++)
     for (j=1; j<=n-i; j++)
       if (a[j] > a[j+1]) {
          t=a[j]
          a[j]=a[j+1]
          a[j+1]=t
       }
   for (i=1; i<=n; i++)
       v=sprintf("%s",v?v""a:a)
   if (v in x); else print $0
   x[v]=v; v=""
}' file

or Corona's solution in slightly different flavor:

awk '{
  if(a[$1]==$2 || a[$2]==$1) next; 
  a[$1]=$2;  print
}' data

comes out unsorted.

Don't know how the input file can look like but with the given example, the following works :

fold -w 1 inputfile | sort | uniq | xargs -n2

---------- Post updated at 12:25 AM ---------- Previous update was at 12:22 AM ----------

fold -w 1 inputfile | sort -u | xargs -n2

This would miss entries if you have multiple occurrences in one column:

$ cat data
a b
b a
e f
c d
f e
d c
a c
$ fold -w 1 data | sort | uniq | xargs -n2
a b
c d
e f

godzilla07 did not specify the criteria nor the logic needed to get the output of his example, so i am aware that the code i provide may not fit all cases, but i shot it as is since no futher constraints has been specified so far.

i didn't mean to be critical, just pointing out...

@mirni

No problem dude :smiley: