awk - compare 2 files and print columns from both files

dani_anyman

I posted something similar a while ago and I thought, the code provided could help in solving my problem, however unfortunately I am not able to adjust it to my needs: awk - compare files and print lines from both files

So, I have again 2 tab-separated files.

file_1.txt

apple    2.5    5     7.2
great    3.8    10    3.6
see      7.6    3     4.9
tree     5.4    11    5
back     8.9    2     2.1

file_2.txt

apple    :::N
back     :::ADJ
back     :::N      
around   :::ADV      
great    :::ADJ         
bee      :::N         
see      :::V      
tree     :::N         

The output should look like:

apple    :::N      2.5    5     7.2     
great    :::ADJ    3.8    10    3.6
back     :::ADJ    8.9    2     2.1
back     :::N      8.9    2     2.1
see      :::V      7.6    3     4.9
tree     :::N      5.4    11    5 

The difference to the other post is, that I just like to compare the first columns of file_1.txt and file_2.txt and then print the whole line of file_1.txt with column 2 of file_1.txt to the outfile. I do not care in which order $2 of file_2.txt is printed to the outfile, so the outfile could as well look like

back     8.9    2     2.1    :::N
back     8.9    2     2.1    :::V etc.

The problem are the duplicates in column1 as back here. Otherwise I could of course just use paste. The problem with this `awk-command is, that it does not read column2 in the a array and if I tell it to print it, this is not possible of course.

awk 'NR==FNR {a[$1]; next} $1 in a {print $0, a[$2]}' OFS='\t' file_2.txt file_1.txt > outfile.txt

I am gladly appreciating any help! Sorry for the stupidity here also, seems that I am completely stumped.

steeldriver

If you have GNU awk (available from the repository via package gawk), which supports multi-dimensional arrays, you could do

gawk 'NR==FNR {a[$1][$2]++; next} $1 in a {for (x in a[$1]) print $0, x}' OFS="\t" file_2.txt file_1.txt

Ex.

$ gawk 'NR==FNR {a[$1][$2]++; next} $1 in a {for (x in a[$1]) print $0, x}' OFS="\t" file_2.txt file_1.txt
apple   2.5     5       7.2     :::N
great   3.8     10      3.6     :::ADJ
see     7.6     3       4.9     :::V
tree    5.4     11      5       :::N
back    8.9     2       2.1     :::ADJ
back    8.9     2       2.1     :::N

Otherwise, if output order is not important the easiest solution is probably to use the join command instead:

$ join -t $'\t' <(sort file_1.txt) <(sort file_2.txt)
apple   2.5     5       7.2     :::N
back    8.9     2       2.1     :::ADJ
back    8.9     2       2.1     :::N
great   3.8     10      3.6     :::ADJ
see     7.6     3       4.9     :::V
tree    5.4     11      5       :::N

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

awk - compare files and print lines from both files

From Dev

Using awk to compare files and combine output from both files?

From Dev

Compare 2 files with awk and substract value from one column if the value of another column matches in both files

From Dev

awk search column from one file, if match print columns from both files

From Dev

Compare second column of two text files and print first columns of both files if match

From Dev

awk compare 2 files, print match and nonmatch lines

From Dev

awk compare 2 files, print match and nonmatch lines

From Dev

awk - comparing 2 columns of 2 files and print common lines

From Dev

join columns but print all lines in both files

From Dev

AWK compare two columns in two seperate files

From Dev

Compare columns conditionally between two files in awk

From Dev

Compare 2 files and keep entries that are not common to both

From Dev

How to compare 2 .csv files and create a new .csv containing parts from both csv files?

From Dev

Compare two files of different columns and print different columns

From Dev

Compare two csv files and add columns that are not common in both of them

From Dev

Compare 2 Columns in 2 different files

From Dev

awk: print duplicates from two files

From Dev

awk to compare two files

From Dev

Awk: compare two files

From Dev

AWK Compare Two files

From Dev

awk compare two files and print first field in file 1

From Dev

Merge and compare different columns from different files

From Dev

How to compare two csv files by 2 columns?

From Dev

AWK to average over columns from multiple files

From Dev

Combine columns from multiple files using awk

From Dev

Compare 2 csv files and match based on 1 column then export new file that contains fields from both

From Dev

match and print multiple columns from two files

From Dev

Match Columns Between 2 Files and Print

From Dev

Compare two files' columns

Related Related

  1. 1

    awk - compare files and print lines from both files

  2. 2

    Using awk to compare files and combine output from both files?

  3. 3

    Compare 2 files with awk and substract value from one column if the value of another column matches in both files

  4. 4

    awk search column from one file, if match print columns from both files

  5. 5

    Compare second column of two text files and print first columns of both files if match

  6. 6

    awk compare 2 files, print match and nonmatch lines

  7. 7

    awk compare 2 files, print match and nonmatch lines

  8. 8

    awk - comparing 2 columns of 2 files and print common lines

  9. 9

    join columns but print all lines in both files

  10. 10

    AWK compare two columns in two seperate files

  11. 11

    Compare columns conditionally between two files in awk

  12. 12

    Compare 2 files and keep entries that are not common to both

  13. 13

    How to compare 2 .csv files and create a new .csv containing parts from both csv files?

  14. 14

    Compare two files of different columns and print different columns

  15. 15

    Compare two csv files and add columns that are not common in both of them

  16. 16

    Compare 2 Columns in 2 different files

  17. 17

    awk: print duplicates from two files

  18. 18

    awk to compare two files

  19. 19

    Awk: compare two files

  20. 20

    AWK Compare Two files

  21. 21

    awk compare two files and print first field in file 1

  22. 22

    Merge and compare different columns from different files

  23. 23

    How to compare two csv files by 2 columns?

  24. 24

    AWK to average over columns from multiple files

  25. 25

    Combine columns from multiple files using awk

  26. 26

    Compare 2 csv files and match based on 1 column then export new file that contains fields from both

  27. 27

    match and print multiple columns from two files

  28. 28

    Match Columns Between 2 Files and Print

  29. 29

    Compare two files' columns

HotTag

Archive