I have a list of discrete elements that I want to test inclusion of an entry from each line of my file. I'd like a succinct way to create a list or array in awk and then test each line against that list.
My list of discrete elements:
ports=(1010, 2020, 3030, 8888, 12345)
myFile:
127.0.0.1 1010
127.0.0.1 1011
127.0.0.1 12345
127.0.0.1 3333
My pseudocode:
awk '
BEGIN {
test_ports=[1010, 2020, 3030, 8888, 12345]
}
($2 in test_ports) {
print $0
}
' myFile
The code below works, but it is not succinct and I don't like how it grows as the list grows, like if I get 100 ports to test against, or 1000...
awk '
BEGIN {
test_ports["1010"]=1
test_ports["2020"]=1
test_ports["3030"]=1
test_ports["8888"]=1
test_ports["12345"]=1
}
($2 in test_ports) {
print $0
}
' myFile
Something like this would be good too, but the syntax isn't quite right:
for i in 1010 2020 3030 8888 12345 {test_ports[i]=1}
EDIT
This code works too and is very close to what I need, but it still seems a bit long for what it's doing.
awk '
BEGIN {
ports="1010,2020,3030,8888,12345"
split(ports, ports_array, ",")
for (i in ports_array) {test_ports[ports_array[i]] = 1}
}
($2 in test_ports) {
print $0
}
' myFile