Monday, February 27, 2012

`grep` for "an ipv4 ip address"

That means return lines with normal (ddd.ddd.ddd.ddd style) ip addresses:

cat .bash_history | grep -E '[0-9]{1,3}\.[1-9]{1,3}\.[1-9]{1,3}\.[1-9]{1,3}'
 I was looking for a server I connected to a few days ago, but didn't want to search through everything manually. I'm no regex master, so I am SURE there's a better way (maybe grouping that first one and saying {3}? ...

haha yep:
[nak@arch ~]$ cat .bash_history | grep -E '([0-9]{1,3}\.){3}'


  1. This comment has been removed by the author.

  2. You can catch a complete dotted quad including the last octet with a slight variation of your last grep:

    grep -E '([0-9]{1,3}.){3}[0-9]{1,3}'

    This way you will only catch full IPV4 addresses and not junk that could be placed in as logs such as software version numbers or dates (3.2.1 or 03.10.12).