Skip to content

Instantly share code, notes, and snippets.

@vietor
Created August 6, 2014 02:28
Show Gist options
  • Save vietor/aec451638ef19f447fda to your computer and use it in GitHub Desktop.
Save vietor/aec451638ef19f447fda to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ ! -f "$1" ]; then
echo "File not found!"
exit
fi
HOSTFILE=$1
function reverse {
echo "$1" | awk '{ for(i=length;i!=0;i--)x=x substr($0,i,1);}END{print x}'
}
N=0
M=0
ARRAY=()
MAP_ARRAY=()
while read LINE ; do
N=$((N+1))
if ! echo $LINE | grep -E '(^[:space:]*#)|(^[:space:]*$)' > /dev/null; then
arr=($LINE)
if [[ ${#arr[@]} = 2 ]]; then
M=$((M+1))
ARRAY+=($(reverse "${arr[1]}"))
MAP_ARRAY+=("${arr[1]}|${arr[0]}")
elif [[ ${#arr[@]} > 2 ]]; then
M=$((M+1))
for (( i=1; i<${#arr[@]}; i++ )); do
ARRAY+=($(reverse "${arr[$i]}"))
MAP_ARRAY+=("${arr[$i]}|${arr[0]}")
done
fi
fi
done < $HOSTFILE
ARRAY=($(printf '%s\n' "${ARRAY[@]}"|sort))
for domain in ${ARRAY[@]}; do
rdomain=$(reverse "$domain")
for item in ${MAP_ARRAY[@]}; do
KEY=${item%%|*}
VALUE=${item#*|}
if [[ $rdomain == $KEY ]]; then
echo "$VALUE $rdomain"
break;
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment