[vox-tech] bash scripting question

Bill Broadley bill at cse.ucdavis.edu
Wed Apr 25 22:44:06 PDT 2007


Micah Cowan wrote:
> Cylar Z wrote:
>> Hey programming gurus...
>>  
>> I want to write a script which takes a block of text
>> and extracts any numbers which match a 123.456.789.012
>> pattern. I am not looking for any numbers in
>> particular (so I don't think the grep command will be
>> of much help) but rather, any set of numbers that
>> looks like an IP address. 
> 
> Why /not/ grep?
> 
> egrep -o '\<[0-9]{1,3}(\.[0-9]{1,3}){3}\>'
> 
> does a decent job, though it will match invalid components like your 456
> or 789 above. A more thorough regex could be constructed to match
> exactly 0-255, but it's somewhat painful.
> 

Heh, exactly, I was just working out similar when I saw the post.

If you do not want duplication then:
egrep -o '\<[0-9]{1,3}(\.[0-9]{1,3}){3}\>' | sort | uniq


More information about the vox-tech mailing list