Compare two arrays in different columns in a file and print matching elements for every row using unix

Perceval Vellosillo Gonzalez

I have a tabulated file like

col1 col2 6 29 61 63 67 70 133 134 150 159 166 208 220 260 261 262 303 312 316 327 330 349 378 387 396 408 415 454 465 V 260 135 49 159

and so on up to thousand rows.

divided in five columns. I have converted third and fifth column in arrays by means of split (space delimiter) in order to compare both of them and print matching numbers. However I have tried different ways without results by the following code

awk 'BEGIN {FS=OFS="\t"} { allpos=split($3,arr1," "); posSNP=split($5,arr2," "); { for (j in arr2) {for (i in arr1) { if ( arr2[j] == arr1[i]) {printf "%s ", i arr1[i]}} printf "\n"}}}' "input" > "output";

and similar codes.

My desired output and would be something like:

col1 col2 V: 159 - 260

How can I get it in unix environment? Thanks in advance

karakfa

a hash look up will be faster, you can further optimize by using the lengths to pick the hashed one.

awk 'BEGIN {FS=OFS="\t"}                                                                  
           {n=split($3,a3," ");
            m=split($5,a5," ");
            for(i=1;i<=m;i++) a[a5[i]];
            SEP=""
            for(i=1;i<=n;i++) if(a3[i] in a) {both=both SEP a3[i]; SEP="-"}
            print $1,$2,$4 ":" both }' file

col1    col2    V:159-260

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Compare two arrays and print number of matching elements

compare two file and print the lines that have matching columns awk

how to compare multiple columns and print not matching columns using unix shell script

Compare two columns from two different files and print non-matching and new separately

compare and print the values in two arrays using awk

How to compare the columns of file1 to the columns of file2, select matching values, and output to new file using grep or unix commands

How to compare two arrays by multiple columns (row with row)

How to compare two column of two file and print not matching pattern with awk

Compare two numpy arrays and return matching nth element of row

Compare two different length arrays using lodash

How can I compare different columns in two files, and print out in first file?

Replace matching elements within two arrays with a different value using for loops and if conditions in python 3

Compare two arrays and print out Index of row in python

Compare two columns in different files and append data for the shared items - UNIX

Compare three files using two columns and print unique entries in each file using awk/sed

Compare two columns with no header of same csv file and output matching values using Python 3.8

Compare two files with the third column in each file matching but the second fith columns do not match using awk

Compare consecutive columns of a file and return the number of non-matching elements

Compare two files and print matching line in the output file

compare two file for match, print only one if duplicate matching found

compare two file for match, print all matching found

How to compare two dataframe and print columns that are different in scala

Compare two arrays of different structures and find elements in PHP

How to compare two arrays containing different data type elements?

JS compare the order of SOME elements in two different arrays

Compare elements in two 1D arrays of different size, with tolerance

Compare two arrays with forEach and add different html elements on condition

compare columns from two different files and PRINT RECORDS FROM FIRST FILE those that DO NOT match from second file

unix - compare columns of two files