Skip to content

Instantly share code, notes, and snippets.

@drldcsta
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drldcsta/3165dbff89a9cef8d6ed to your computer and use it in GitHub Desktop.
Save drldcsta/3165dbff89a9cef8d6ed to your computer and use it in GitHub Desktop.
Checking for existence of an element in an array in awk
c82a1446be6c:~ decostad$ echo "1,2,3"|awk -F, '{{split($0,arr,",")};if ("1" in arr){print "found it"}}'
found it
c82a1446be6c:~ decostad$ echo "1,2,3"|awk -F, '{{split($0,arr,",")};if ("2" in arr){print "found it"}}'
found it
c82a1446be6c:~ decostad$ echo "1,2,3"|awk -F, '{{split($0,arr,",")};if ("3" in arr){print "found it"}}'
found it
c82a1446be6c:~ decostad$ echo "1,2,3"|awk -F, '{{split($0,arr,",")};if ("4" in arr){print "found it"}}'
c82a1446be6c:~ decostad$ echo "1,2,3"|gawk --profile -F, '{{split($0,arr,",")};if ("4" in arr){print "found it"}}'
c82a1446be6c:~ decostad$ cat awkprof.out
# gawk profile, created Tue Apr 7 17:53:14 2015
# Rule(s)
1 {
1 split($0, arr, ",")
1 if ("4" in arr) {
print "found it"
}
}
c82a1446be6c:~ decostad$ echo "1,2,3"|gawk --profile '{{split($0,arr,",")};if (!("4" in arr)){print "not there"}}'
not there
c82a1446be6c:~ decostad$ cat awkprof.out
# gawk profile, created Tue Apr 7 17:57:22 2015
# Rule(s)
1 {
1 split($0, arr, ",")
1 if (! ("4" in arr)) { # 1
1 print "not there"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment