Skip to content

Instantly share code, notes, and snippets.

@TomTriple
Created November 12, 2016 14:54
Show Gist options
  • Save TomTriple/bf8e2749573d1c935c42e86f52bebc18 to your computer and use it in GitHub Desktop.
Save TomTriple/bf8e2749573d1c935c42e86f52bebc18 to your computer and use it in GitHub Desktop.
bash_script.example
#####################################################################
# 1. the input for the script at (2) -- e.g. from a database query
#####################################################################
#
# client_addr | count
#----------------+-------
# 172.217.16.35 | 45
# | 1
# 62.153.159.92 | 15
# 217.72.219.144 | 24
#(9 rows)
#
################################################
# 2. apply classic pipes & filters on the input
################################################
#!/usr/bin/env bash
result=$(tail -n +3 < data.txt | awk 'BEGIN{FS="|"}{print $1,$2}')
echo "$result" | grep -iv rows | while read ip count; do
if [ "$ip" != '' ] && [ "$count" != '' ]; then
name=$(nslookup $ip | sed -n -e 's/^.*name = \(.*\)$/\1/p' | head -n 1)
echo "$name $count"
fi
done | awk 'BEGIN{count=0}{count+=$2}{print $0}END{print "Gesamt", count}'
#######################
# 3. the output
#######################
$ ./execute_the_above_script.sh
muc03s08-in-f35.1e100.net. 45
www.t-online.de. 15
144.boerse.de. 24
Gesamt 84
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment