elonden's awk script is pretty close, actually. Just switch the two files, and it becomes pretty simple.
Code:
awk -v keyfile=pl '
BEGIN {
FS = "," ;
while ((getline < keyfile) > 0)
if ($1 != "")
keys[$1] = 1 ;
close(keyfile)
}
($1 in keys) { print $0 }
' ns
I added the semicolons so you can put it all on one line if you want.
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):
Code:
gawk -v keyfile=pl '
BEGIN {
RS = "[\n\r]+" ;
FS = "," ;
RT = "\n" ;
while ((getline < keyfile) > 0)
if ($1 != "")
keys[$1] = 1 ;
close(keyfile)
}
($1 in keys) { printf("%s%s", $0, RT) }
' ns
The same script works for other awk variants too, but they do not retain the newline convention (output will always use UNIX newlines, "\n"), only gawk does. (GNU awk provides an automatic variable
RT, which contains the pattern that matched the record separator RS for the current record. Other awk variants treat
RT as a normal variable.)