简体   繁体   中英

compare strings two files with awk

I am trying to compare field 5 from file1 with field 2 from file2 and show lines when they are equal.

file1

"202005";"RS";"4301602";"CITY";"12651500714";"***.368.660-**";"TESTE TESTE";"-2";""
"202005";"RS";"4301602";"BAGE";"00000000000";"***.977.440-**";"XXXXX XXXEZ";"-2";""

file2

"93330090006";"12651500714";"TESTE XX";
"01168199018";"16108835006";"SOME NAME";

I searched and used a lot of codes, but it's not working. My last code:

awk -F";"  'NR==FNR{a[$4,$2];next} ($0) in a' file1 file2

The expected result can be just entire line from file1:

"202005";"RS";"4301602";"CITY";"12651500714";"***.368.660-**";"TESTE TESTE";"-2";""

You may try this code:

awk 'BEGIN{FS=";"} FNR == NR {seen[$2]; next} $5 in seen' f2 f1
"202005";"RS";"4301602";"CITY";"12651500714";"***.368.660-**";"TESTE TESTE";"-2";""

This command will only print lines from file2 where 5th column from file1 is same as 2nd column from file2.

An alternate solution is to use the *nix utility join .

 join -t\; -1 5 -2 2 -o 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 f1.txt f2.txt

produces the output

"202005";"RS";"4301602";"CITY";"12651500714";"***.368.660-**";"TESTE TESTE";"-2";""

Note that both files need to be sorted by their key field. If your data needs sorting, this can be accomplished in-line using process substitution:

join -1 5 -2 2 -t\; <(sort -t\; -k5 f1.txt)  <(sort -t\; -l2 f2.txt)

Note also the flexibility of join in that you specify the key field number after the file number, ie, -1 5 in this case.

Finally, note that if you wanted the "joined" output of both files the command would be simpler, ie

join -t\; -1 5 -2 2 f1.txt f2.txt

producing the output of the first file and the second file on one line. Note that the key field is not including in the file 2 output.

"12651500714";"202005";"RS";"4301602";"CITY";"***.368.660-**";"TESTE TESTE";"-2";"";"93330090006";"TESTE XX";

For beginners, the man join is rather a terse description of the features. Look for online tutorials that match your learning style, or search here, as there are many Q/As on the subject (often as an answer to a similar Q).

IHTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM