Security and Technology Ramblings…

I am by far NOT a scripting god. Not even close. So, I find myself proud that I was able to create this script.

I am only placing it here on my blog so I will not forget this new found knowledge. If it is helpful to you, cool. If it is way beneath you, ignore my newbee like ways.

This will read in the contents of a file called by expression $1 and fix all the leading zero’s in an IP address.


while read ip
do
a="$(echo $ip | awk -F'.' '{print $1}')"
b="$(echo $ip | awk -F'.' '{print $2}')"
c="$(echo $ip | awk -F'.' '{print $3}')"
d="$(echo $ip | awk -F'.' '{print $4}')"
if [ $a = "000" ]; then
  aa="0"
elif [ $a = "00" ]; then
  aa="0"
else
aa="$(echo $a | sed 's/^0*//')"
fi
if [ $b = "000" ]; then
        bb="0"
elif [ $b = "00" ]; then
        bb="0"
else
bb="$(echo $b | sed 's/^0*//')"
fi
if [ $c = "000" ]; then
        cc="0"
elif [ $c = "00" ]; then
        cc="0"
else
cc="$(echo $c | sed 's/^0*//')"
fi
if [ $d = "000" ]; then
        dd="0"
elif [ $d = "00" ]; then
        dd="0"
else
dd="$(echo $d | sed 's/^0*//')"
fi
echo $aa.$bb.$cc.$dd
done < $1

Leave a Reply