Search code examples
bashshellawkipcidr

How to process CIDR block/Text processing?


I've to process CIDR block and add 2 to the last octet in single line to use it inside of a shell script. For example, if my CIDR block is 10.0.0.0/8, then my output should be 10.0.0.2 only.


Solution

  • Assuming your line only contains the IP address and block, such as:

    BLOCK=10.0.0.0/8
    

    You can use awk:

    cut -d/ -f1 <<< "$BLOCK" | awk -F . 'BEGIN {OFS="."} {print $1,$2,$3,$4+2}'