Last active
August 29, 2015 14:18
-
-
Save drldcsta/3165dbff89a9cef8d6ed to your computer and use it in GitHub Desktop.
Checking for existence of an element in an array in awk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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