Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 "
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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'`
share|improve this answer
    
I use the ksh shell not the bash shell –  yael Feb 7 '13 at 11:43
    
@yael: many versions of ksh also support this type of parameter expansion. –  Thor Feb 7 '13 at 11:46
    
my ksh not support it - # ifconfig -a | /usr/xpg4/bin/grep "${IP_ADDRESS//./\\.}" ksh: "${IP_ADDRESS//./\\.}": bad substitution –  yael Feb 7 '13 at 11:47
    
@yael: alternatively you could use shell expansion, see edit. –  Thor Feb 7 '13 at 17:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.