awk compare 2 files, print match and nonmatch lines

SVR

In this example, need to compare two files f1.txt and f2.txt and obtain matches, and non-matches, for this case I am looking to match 2nd field of second file and 1st field of first file. And print first the second field of f2.txt, then print the entire line of f1.txt. And for no match found on f2.txt to state "Not Found" and then print f1.txt entire line.

F1.txt

1;2;3;4;5;6;7;8 
1a;2;3;4;5;6;7;8 
1b;2;3;4;5;6;7;8 
2b;2;3;4;5;6;7;8

F2.txt

First;1 
Firsta;1a 
Firstb;1b

Desired Output:

First;1;1;2;3;4;5;6;7;8
Firsta;1a;1a;2;3;4;5;6;7;8
Firstb;1b;1b;2;3;4;5;6;7;8
Not Found;Not Found;2b;2;3;4;5;6;7;8

Tried but not working:

awk -F";" 'NR==FNR{a[$1]=$0;b[$1]=$0;next}{ if (a[$2]=b[$1]) print a[$1],$0;else print "NotAvailable","NotAvailable", $0;}' OFS=";" f2.txt f1.txt
user000001

You were close. Here is the correct script:

$ awk -F ";" 'NR==FNR{a[$2]=$0;next}{print (a[$1]?a[$1]:"Not Found;Not Found") ";" $0}' F2.txt F1.txt 
First;1;1;2;3;4;5;6;7;8
Firsta;1a;1a;2;3;4;5;6;7;8
Firstb;1b;1b;2;3;4;5;6;7;8
Not Found;Not Found;2b;2;3;4;5;6;7;8

Take care to remove any trailing spaces from the input file.

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 2 files, print match and nonmatch lines

From Dev

awk compare 2 files, print match and nonmatch lines;3rd column of first file and 2nd column of second file

From Dev

awk print 2 lines back if match

From Dev

awk - compare files and print lines from both files

From Dev

awk - compare 2 files and print columns from both files

From Dev

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

From Dev

compare 2 files, print lines of second file in the order of first file

From Dev

awk Lookup 2 files, print match and Sum of Sencond Field:

From Dev

Read two csv files and compare every line. If the lines match print both lines, if it isn't similar print invalid

From Dev

awk: print lines after match to end of file

From Dev

awk exact variable match, print remaining lines

From Dev

Compare two files and print only the first word of the lines which don't match along with a string

From Dev

How to compare two column of two file and print the number of match with awk

From Dev

How to compare two column of two file and print the number of match with awk

From Dev

Print lines from match until end of file with awk

From Dev

awk find the last match and print the next N lines

From Dev

awk + print lines from the first line until match word

From Dev

awk find the last match and print the next N lines

From Dev

awk print only lines between two patterns removing first match

From Dev

awk: print lines that DO NOT match patterns in a file, looking at a specific column

From Dev

awk compare two files and print first field in file 1

From Dev

compare two lines and print unmatched words from two files

From Dev

Compare two fields of two files and print if they do not match

From Dev

Compare two fields of two files and print if they do not match

From Dev

awk lines not match

From Dev

awk match lines that has a "(" but no ")"

From Dev

AWK Column Match in Two Files, Print Different Column

From Dev

Awk: Match data in 2 files with duplicate keys

From Dev

Awk print $0 print all lines of my files to the last position of column ;how I can print at first?

Related Related

  1. 1

    awk compare 2 files, print match and nonmatch lines

  2. 2

    awk compare 2 files, print match and nonmatch lines;3rd column of first file and 2nd column of second file

  3. 3

    awk print 2 lines back if match

  4. 4

    awk - compare files and print lines from both files

  5. 5

    awk - compare 2 files and print columns from both files

  6. 6

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

  7. 7

    compare 2 files, print lines of second file in the order of first file

  8. 8

    awk Lookup 2 files, print match and Sum of Sencond Field:

  9. 9

    Read two csv files and compare every line. If the lines match print both lines, if it isn't similar print invalid

  10. 10

    awk: print lines after match to end of file

  11. 11

    awk exact variable match, print remaining lines

  12. 12

    Compare two files and print only the first word of the lines which don't match along with a string

  13. 13

    How to compare two column of two file and print the number of match with awk

  14. 14

    How to compare two column of two file and print the number of match with awk

  15. 15

    Print lines from match until end of file with awk

  16. 16

    awk find the last match and print the next N lines

  17. 17

    awk + print lines from the first line until match word

  18. 18

    awk find the last match and print the next N lines

  19. 19

    awk print only lines between two patterns removing first match

  20. 20

    awk: print lines that DO NOT match patterns in a file, looking at a specific column

  21. 21

    awk compare two files and print first field in file 1

  22. 22

    compare two lines and print unmatched words from two files

  23. 23

    Compare two fields of two files and print if they do not match

  24. 24

    Compare two fields of two files and print if they do not match

  25. 25

    awk lines not match

  26. 26

    awk match lines that has a "(" but no ")"

  27. 27

    AWK Column Match in Two Files, Print Different Column

  28. 28

    Awk: Match data in 2 files with duplicate keys

  29. 29

    Awk print $0 print all lines of my files to the last position of column ;how I can print at first?

HotTag

Archive