0

I need to grep to IP address ( as the following example ) , I use ksh script ,

    #  ifconfig -a |  /usr/xpg4/bin/grep   "100\.106\.2\.120 "
    inet 100.106.2.120 netmask ffffff00 broadcast 100.106.2.255

but how to grep IP address from parameter ( IP_ADDRESS ) ?

  • Remark - IP address could be any IP

how to add the back slash in this case

IP_ADDRESS=192.2.34.2 , or IP_ADDRESS=192.2.34.20 ... etc

    #  ifconfig -a |  /usr/xpg4/bin/grep   "$IP_ADDRESS "
yael
  • 2,433
  • 5
  • 31
  • 43

1 Answers1

3

If you are using the bash shell you can Use parameter expansion:

ifconfig -a | /usr/xpg4/bin/grep "${IP_ADDRESS//./\\.}"

If your shell doesn't support this type of parameter expansion, you could use shell expansion instead, e.g. with sed:

ifconfig -a | /usr/xpg4/bin/grep `echo $IP_ADDRESS | sed 's/\./\\./g'`

or perl:

ifconfig -a | /usr/xpg4/bin/grep `echo $IP_ADDRESS | perl -pne 's/\./\\./g'`
Thor
  • 475
  • 5
  • 14