Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a script that generates some output. I want to check that output for any IP address like

159.143.23.12
134.12.178.131
124.143.12.132

if (IPs are found in <file>)
then // bunch of actions //
else // bunch of actions //

Is fgrep a good idea?

I have bash available.

share|improve this question
    
use grep , egrep , awk , or sed , whatever u like – Ijaz Khan yesterday
    
Could you help with the syntax please? I mean how does it search for a random IP. How to describe the pattern? I would be looking for any IP address, not a particular address. – Koshur yesterday
4  
Is that only IPv4 addresses in quad-decimal notation? Could they be written like 0010.0000.0000.0001? May the file otherwise contain things that look like IP addresses like version numbers (soft-1.2.1100.1.tar.gz, network specifications (10.0.0.0/24), 1.2.3.4.5)? Would you accept a solution that is positive on 333.444.555.666? Or 0377.0377.0377.0377 (a valid quad-octal IP address)? – Stéphane Chazelas yesterday

Yes , You have lot of options/tools to use. I just tried this , it works:

ifconfig | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"

so you can use grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" to grep the ip addresses from your output.

share|improve this answer
    
Thanks. This works. Can you explain a bit? Is this a regular expression? – Koshur yesterday
    
Yes this is a regular expression used in bash with grep , you are just looking for three digits pattern separated by dots. you can play with by changing the numbers in {1,2} for 2 consecutive digits and so on – Ijaz Khan yesterday

Redirect that output to some outputFile

Simply grep it with pattern as,

grep -sE "159.143.23.12|134.12.178.131|124.143.12.132" <outputFile>
share|improve this answer
4  
Note that . is a regular expression operator and needs escaping for it to be treated literally – Stéphane Chazelas yesterday

If your file is called e.g ips you can write somethinng like:

while read -r ip
    do
        if [[ $ip == "$1" ]]; then
            shift
            printf '%s\n' 'action to take if match found'
        else
            printf '%s\n' 'action to take if match not found'
        fi
    done < ips

Then you can pass the parameters as follow the the script

./myscript 159.143.23.12 134.12.178.131 124.143.12.132 124.143.12.132

share|improve this answer

starting my answer based on this answer:

Yes , You have lot of options/tools to use. I just tried this , it works:

ifconfig | grep -oE "\b([0-9]{1,3}.){3}[0-9]{1,3}\b" a so you can use grep -oE "\b([0-9]{1,3}.){3}[0-9]{1,3}\b" to grep the ip addresses from your output.

and converting the answer to full length IPv6, etc...:

fgrep -oE "\b([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}\b" -- file

if you want to keep the /nnn if it's there:

fgrep -oE "\b([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}(/[0-9]{1,3}){0,1}\b" -- file

and also there's the shortened version of IPv6 that includes '::'.

for more IPv6 answers you can look here: http://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses

share|improve this answer
    
fgrep is the old name for a variant of grep that ignores pattern matching. I'd recommend you use grep (or even egrep) instead, especially as you're clearly wanting pattern matching. – roaima yesterday

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.