AWK Comparing two files
I've been banging my head for a day now but it seems I'm doing something really wrong.
I have two files :
1. file called "pl" containing entries like:
2. file called "ns" containing entries like:
What I want it to check if an entry in file #1 exists in field #1 of file #2 and if NOT either print the output or store it into an array.
Any feedback would be welcome.
What i have is the following (and obviously doesn't work :-)):
If the given pattern only exists once on the line, how about this instead?
elonden's awk script is pretty close, actually. Just switch the two files, and it becomes pretty simple.
The BEGIN rule reads in the keys from keyfile (pl), and creates an associative array out of them. The important bit is that you populate the keys in the keys array. (The value is irrelevant here, but often useful. You might use e.g. keys[$1]=++nkeys instead if you later extend the script and need to keep track of which key caused the record to be output.)
The rule ($1 in keys) is considered for each input record (line) of the ns file. It applies to all records where the first field matches one of the keys in the keys array. The body of the rule just prints the entire record.
If your input files may contain any newline convention (i.e. they may be produced in various operating systems, including Windows and old Macs), and you wish to retain that convention, extend the script a bit, and use GNU awk (gawk):
|All times are GMT -5. The time now is 04:13 PM.|