I write the following syntax (exist in my ksh script) in order to find how many digits I have in IP address
IP_address=127.1.1.1
echo $IP_address | sed s'/\./ /g' | wc | awk '{print $2}'
4
.
IP_address=127.1.1
echo $IP_address | sed s'/\./ /g' | wc | awk '{print $2}'
3
Please advice how to improve my syntax to make it - simpler and shorter and faster? (prefer without echo command ) , we can use also perl or awk or sed etc
Example what I defined in my code
if [[ ` echo $IP_address | sed s'/\./ /g' | wc | awk '{print $2}' ` -eq 3 ]]
then
three_octat=true
elif [[ ` echo $IP_address | sed s'/\./ /g' | wc | awk '{print $2}' ` -eq 4 ]]
then
three_octat=false
fi
echo $IP_address | awk -F. '{print NF}'
- essentially define awk's field separator (-F
) as '.
' and display (print
) the number of fields (NF
).tr
instead ofsed
if you just want to swap one character for another.