Skip to content
Advertisement

AWK: Comparing substrings from two files and write to third file

I’m trying to compare two different files, let’s say “file1” and “file2”, in this way. If the substring of characters i.e 5 characters at position (8 to 12) matches in both files – file1 and file2, then remove that matching row from file 1. Finally, write the output to file3.(output contains the remaining rows which are not matching with file 2) My output is the non matching rows of file1. Output (file3) = File1 – File2

JavaScript

I tried awk but i need some thing which looks at substring of the two files, since there are no delimiters in my files. $ awk ‘FNR==NR {a[$1]; next} $1 in a’ f1 f2 > file3

Advertisement

Answer

Could you please try following, written and tested with shown samples in GNU awk. Once happy with results on terminal then redirect output of following command to > file3(append > file3 to following command).

JavaScript

Explanation: Adding detailed explanation for above.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement