Skip to content

Instantly share code, notes, and snippets.

@huangsam
Created January 20, 2019 01:40
Show Gist options
  • Save huangsam/fd9462a39dd4f002d26d1af0388951c5 to your computer and use it in GitHub Desktop.
Save huangsam/fd9462a39dd4f002d26d1af0388951c5 to your computer and use it in GitHub Desktop.
Awk in action
#!/bin/bash
find . -name 'meta-*.yml' | xargs awk -F':' -f metadata.awk
- ip: 10.17.13.105
hostname: spider.pic
expired: false
- ip: 8.8.8.8
hostname: www.google.com
expired: false
- ip: 10.17.11.101
hostname: spider.pic
expired: false
- ip: 8.8.8.9
hostname: www.google.com
expired: false
BEGIN {
print "== metadata"
found = 0;
}
/ip:/ {
gsub(/ /, "", $NF);
printf "ip: [%s] | match: [%s]\n", $NF, $0;
found++;
}
/expired:/ {
gsub(/ /, "", $NF);
printf "ip: [%s] | match: [%s]\n", $NF, $0;
found++;
}
END {
if (found > 10) {
condition = "great"
} else if (found > 7) {
condition = "good"
} else if (found > 5) {
condition = "so-so"
} else {
condition = "bad"
}
printf "%s - %s\n", found, condition
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment