Matching text using grep

Hi folks...

Relatively new to scripting, but really struggling with something that will no doubt be second nature to most people on here:

Trying to get an exact match on $sub, where sub is an ip address.

subnet () 
{  
   clear         
   while true         
   do                 
      echo "\n "                 
      echo "Please enter the subnet to be changed: \c"                 
      read sub                 
      grep -w "$sub" subnet                 
      if [[ $? -eq 0 ]]                 
      then                         
           print "Matched"                         
           break;                 
      else 
           echo "\n\n\tNot a valid subnet - please try again.";                 
      fi         
   done 
}

As a testing mechanism I've created a file called subnet, containing 111.222.333.0, but no matter what I seem to do, I can't get it to match and "only" match the exact content of subnet.

Any one able to help?

Thanks
Cica

Make it clear on the format of the data in the file subnet on which you are doing a grep?

This works for me

grep -i "$ip" file

Please show an example of your input and expected output using code tags, thanks.

The above code works in bash - although I'm using ksh in this instance.

# cat subnet
111.222.333.0
222.111.333.0
333.222.111.0

is like that a problem ?

# grep -w "111" subnet
111.222.333.0
222.111.333.0
333.222.111.0
# grep -w "^111" subnet
111.222.333.0

note : 333 is not a valid mask value , just for an example input

what if you have 127.0.0.18 and 127.0.0.128 in the file? You will get a match if you look for 127.0.0.1 even if it isn't in there...

grep -i is a wild way of getting some clue for something else and cant be trusted when u look for something in particular. And -i is for case insensitive search and will not hold for numerals and special characters.

!?!?!

Sorry for the lack info/clarity...

Basically the subnet file will contain a list of subnets where the first to values (111.222 in this case) will not change so entries could be as follows:

111.222.xxx.x
or
111.222.xx.x
or
111.222.xxx.xx

etc etc.

So for example if the subnet was 111.222.15.0 (and making the assumption that there is also a .115 subnet present) a grep will return both:

111.222.15.0 and 111.222.115.0

What I'm struggling to do is pick out the .15.0 subnet only

NOTE - 111.222.333 is just being used as examples :slight_smile:

what is your operating system and grep version ?

Solaris 10 and grep is SUNWggrp 11.10.0.

Also I'm trying to write the above code in ksh

i try some but result seem like correct..
maybe let check the this in ksh and bash in your script

..........
read sub

echo "`ps -ef|grep $PPID|grep -v grep`"
echo "SHEBANG : `/usr/xpg4/bin/sed -n '1s/^#!/&/p' $0`"
echo "grep : `which grep`"
echo "sub value : $sub"

...........

what is the outputs ?

Try:

grep '\.15\.0$'

The dot has a special meaning in regex and needs to be escaped...

Output:

Please enter the subnet to be changed: 111.222.333.0
root 3765 3755 0 Jun 29 pts/2 0:02 -bash
root 20968 3765 0 13:31:46 pts/2 0:00 /bin/ksh ./generate-batchlist
SHEBANG : #!/bin/ksh
grep : /usr/bin/grep
sub value : 111.222.333.0
0

---------- Post updated at 01:37 PM ---------- Previous update was at 01:35 PM ----------

Scruitinizer - that would be fine, although the problem is, the subnet address is read in via the $sub variable and won't always be .15

Then try:

grep "${sub//./\\.}$"

in bash / ksh93

Would you change your grep to fgrep and try?

Note: with fgrep you cannot use the $-sign which is required for exact matching..

Apologies for the delayed reply...

Scrutinizer - what am I missing here:

-- snip --

subnet ()
{

clear
        while true
        do
                echo "\n "
                echo "Please enter the subnet to be changed: \c"
                read sub
                grep "${sub//./\\.}$"
                echo $?
                if [[ $? -eq 0 ]]
                then
                        print "Arrived"
                        break;
                else
                        echo "\n\n\tNot a valid subnet - please try again.";
                        echo $?
                fi
        done
}

--snip--

Please enter the subnet to be changed: 111.222.333.0
./generate-batchlist[9]: "${sub//./\\.}$": bad substitution

You have not specified the file after the grep statement...
But there is another problem here, your shell does not understand that kind of substitution. You would need to use /usr/dt/bin/dtksh (ksh93) or bash if available.

1 Like

Yup - I think changing to bash is probably the best option here, cause it's been tested in bash and it works...

I'll just need to change all my "print" commands to "echo"

More to follow...