Skip to content

Instantly share code, notes, and snippets.

@gerep
Last active February 12, 2016 17:49
Show Gist options
  • Save gerep/973b3335d63d056aa292 to your computer and use it in GitHub Desktop.
Save gerep/973b3335d63d056aa292 to your computer and use it in GitHub Desktop.
#! /usr/bin/env bash
#
# search [file(s)]
#
# This script will read the files passed as parameters and those files
# will contain a list of IPs separated by lines and will search for
# each of those IPs in both omg-switch log files
#
# When no argument is passed, it will open the page on the current branch
# Fail on any error
set -eu
# Loop throught all arguments
for i in "$@"; do
# On each run an output file will be created, replacing the past one
output="report-"$i
if [ -f $output ]; then
rm $output
fi
touch $output
echo -en "Starting $output\t"
# Reads each line of file with cat
cat $i | while read IP
do
# The grep command searches for the current IP and uses a regular expression
# to return only the serial number
serial=`grep -iw $IP omg-switch.log | awk '{print $3}' | grep -oP '\d+-\d+\-\d+' | sort -u`
# If the current IP is not found in the first log file it will search in the second one
if [ -z $serial ]; then
serial=`grep -iw $IP omg-switch-2.log | awk '{print $3}' | grep -oP '\d+-\d+\-\d+' | sort -u`
fi
echo -e "$IP\t- $serial" >> $output
done
sort -nr -t- -k3 $output -o $output
echo "Done $output"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment