Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

urghhh....grep ip address pattern

Status
Not open for further replies.

aswolff

Programmer
Jul 31, 2006
100
US
I have the following script..but it does not seem to work just the way I want it:

Code:
#!/bin/bash
find . -type f | while read fname; do
grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' "$fname"
#grep '\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}' "$fname"
  if [ $? -eq 0 ]; then
    echo "The above matches came from: $fname"
  fi
done;

The first grep statment returns more records then I need
because I am only looking for patterns such as 127.10.1.2
but it returns stuff such as 1.1.1.1.2

so the the second grep statement is my attempt at fixing the problem...but it does not return anything.

Can somebody spot the problem? I am Sun Solaris.

Thanks.
 
Try grep -w perhaps, i.e. only match whole words?

Also consider the simpler syntax:

Code:
...
if grep ... ${fname}; then
    echo "The above matches came from: $fname"
fi
...


Annihilannic.
 
And this ?
Code:
grep -E '(^|[^.])[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}($|[^.])' "$fname"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
grep -E
returns..
grep: illegal option -- E
Usage: grep -hblcnsviw pattern file . . .
 
Instead of the while loop try this one liner
Code:
find . -type f -exec grep '[^\.][0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}[^\.]' /dev/null {} +
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top