How to remove line `v u` from a file when line `u v` already exists using unix command

user6007040

I have the following test data :

a b
a c
b a
b c
b d
c a
c b
c d
d b
d c

and I want to remove lines v u when line u v already exists using unix command. For example here I want to obtain :

a b
a c
b c
b d
c d

I've tried with an awk script but on a long file it takes too much time :

{
if(NR==1){
    n1=$1
    n2=$2
    test=0
    k=0
    i = 0
    column1[i]=$1
    column2[i]=$2
    printf "%s %s\n", column1[i], column2[i]
}
else{
    for(k=0; k<=i;k++){
        if(column1[k]==$2){
            test=1
            tmp=i
            break
        }
    }
    if(test==1){
        if(column2[tmp]==$1){
            n1=$1
            n2=$2
        }
    }
    else if(n1!=$1||n2!=$2){
        n1=$1
        n2=$2
        i++
        column1[i]=$1
        column2[i]=$2
        printf "%s %s\n", column1[i], column2[i]
    }
    test=0
}
}

Does someone have an idea ?

Tom Fenech

I think this can be achieved pretty simply:

awk '!seen[$1,$2]++ && !seen[$2,$1]' file

This only prints lines (the default action) when the first and second column have not yet been seen in either order.

The array seen keeps track of every pair of fields by setting a key containing the first and second field. The expression !seen[key]++ is only true the first time that a specific key is tested because the value in the array is incremented each time.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to disable Hyper-V in command line?

Unix count line of file command - from java code

Check if Solr Core already exists from Command Line

How to access to U & V matrices from SVD

how to create war file from command line using ansible

How can I remove a line from a csv file using pandas?

How to remove CRLF and a new line from text file using batch command

How to login to Postgres via command line in Docker using secrets, outside container without `docker exec -it <image> psql -h localhost -U postgres`?

Command line: create file with new filename if the filename already exists

How can I specify that curl (via command line) overwrites a file if it already exists?

How can I add line breaks in an XML file from the Unix command line?

How to copy an image to the clipboard from a file using command line?

How to open a file from bash command line in the already open Emacs instead of a new instance?

How to calculate wind direction from U and V wind components in R

How to remove the searching PID from this command line?

How to ignore if line already exists in writing file in python?

Remove line from file in bash script using sed command

How to remove nodes from XML file as command line?

Remove the line if a field of the line exists in another file

How to remove characters from file names using command line?

how to create an audio clip from a video file using command line?

Java : How to read a file by giving the input in command line? I already wrote a code for reading the file line by line

How to escape wildcard in path when using sudo -u <username> command

How to remove all comments from a javascript file using common Linux command line tools?

How to check if Mercurial tag already exists, from command-line?

How to read a config file from a command line using perl (automation)

How to remove a file and directory using command line arguments

How to remove the new line with a v-if

How to download shared file from Alfresco using command line?