15 ms·
One trick I like to do is to feed two files into awk; /dev/stdin and some other file I'm interested in. Here's an example: lookup the subject names of a list of
by sleepydog 13y ago
One trick I like to do is to feed two files into awk; /dev/stdin and some other file I'm interested in. Here's an example: lookup the subject names of a list of serial numbers in an openSSL index.txt
#!/bin/sh
printf %s\\n "$@" | awk -F'\t' '
FILE == "/dev/stdin" {
needle[$0] = 1
next
}
needle[$4] {
print $NF
}
' /dev/stdin /etc/pki/CA/index.txt
I find myself using this idiom (feeding data from a file to awk and selecting it with data from standard input) again and again. It's a great way to scale shell scripts to take multiple arguments while avoiding opening the same file N times, or doing clunky things with awk's -v flag.