Operating System - HP-UX
1828371 Members
2857 Online
109976 Solutions
New Discussion

How to grep the correct pattern?

 
Rashid Ali
Frequent Advisor

How to grep the correct pattern?

I am writing a script to capture all the current users that have established network connection and we have a static ip address list from which we can tell who is using which ip address. But when I grep "128.188.3.15" it come out all like "128.188.3.150, 3.151,3.152,...", May I know how to correct it? The script is shown below,

netstat -n|grep "^tcp"|grep 128|awk '{print $5}'|sed "s/\.[0-9][0-9]*$//" |sort
-u >/tmp/tmpipaddr.lst
while read ipaddr
do
# ipaddr="$ipaddr " ;This line does not help
echo $(grep "$ipaddr " /tmp/ipaddr.txt)
done
16 REPLIES 16
Steven Sim Kok Leong
Honored Contributor

Re: How to grep the correct pattern?

Hi,

One easy way to avoid the search string being interpreted as a substring is to add a space behind your search string ie.

# netstat -n|grep "128.188.3.15 "

instead of

# netstat -n|grep "128.188.3.15"

Similarly, in your search pattern (eg. sed), include the space at the end of your pattern for searching.

Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com
FRED Dennison
Advisor

Re: How to grep the correct pattern?

This script will work, if you use the print commands in the awk statement as follows,...

awk { print $5" " }

Then when you grep for $ipaddr" ", the space character will identify the end of significant data.

Hope this helps, FRED
Peace thru superior firepower.
Rashid Ali
Frequent Advisor

Re: How to grep the correct pattern?

I believed you have not understand what I want.
Actually In my script, it is the command "echo $(grep "$ipaddr " /tmp/ipaddr.txt)" that won't work when searching patterns like "128.188.3.15" in a file /tmp/ipaddr.txt which contains the mapping of ip address and user name, because this will also get all other users info for ip addresses such as "128.188.3.150", "128.188.3.151",etc. I tried to append one space behind the variable $ipaddr but it didn't work. Is there any negative function to filter out some paticular patterns such as "$ipaddr[!0-9]" or "$ipaddr[~0-9]" , so that it can only grep a uniq user info with the required ip address?

I think one of the reason could be that there is some tab/control character behind the ip address instead of space character in file ipaddr.txt. SO how should I pre-process this file before I can run grep "128.188.3.15 " ?
again, how can I apply this rule when the pattern to be searched in grep is assigned to a variable called $ipaddr ? Should it be grep "$ipaddr " /tmp/ipaddr.txt?




federico_3
Honored Contributor

Re: How to grep the correct pattern?

Use:
while read ipaddr
do

echo $(grep "$ipaddr$" /tmp/ipaddr.txt)
done
federico
Carlos Fernandez Riera
Honored Contributor

Re: How to grep the correct pattern?

I wrote it so:

netstat -n | awk ' $1 !="tcp"{ next} { if ( $4 ~ "\.23$") p=1; else if ( $4 ~ "\.1521$") p=2 ;else p=3} $4 ~ "xx\.xxx\.xx\.x1\." { inet[p]+=1 ;next } ...


unsupported
Vincenzo Restuccia
Honored Contributor

Re: How to grep the correct pattern?

for ipaddr in 'cat /tmp/tmpipaddr'
do
grep "$ipaddr\$" /tmp/ipaddr.txt
done
Rajeev Tyagi
Valued Contributor

Re: How to grep the correct pattern?

Hi,

To grep for 128.188.3.15 i am giving you a simple eg which will grep only this ip and only this.

#netstat -n | grep tcp | grep 128 | \ awk '{print $5}' | awk -F"." '{if ($4 == 15) \ print $0}'

Hope this helps.
Wieslaw Krajewski
Honored Contributor

Re: How to grep the correct pattern?

Hi,

Without going deeply into the whole problem, if you want to grep 128.188.3.15 only, not 128.188.3.150 and so on, try simply

grep 128.188.3.15[^1-90]

Of course you can do it the same also in the case when the pattern is given as reference to the value of some variable.

Hope this helps.
Permanent training makes master
Rashid Ali
Frequent Advisor

Re: How to grep the correct pattern?

$ipaddr$ or $ipaddr\$ only matches those patterns that are the end of one line. In my case, it is not necessary true. I prefer "grep 128.188.3.15[^1-90]" ( [^1-90]" can be written as [^0-9] ? ) to rule out other possibilities.

Attached are two files j1 and j2, if you ftp these two files to unix and run "cat", the contents are the same but only grep "3.15 " j2 can work, grep "3.15 " j1 cannot. How come like that? How can I automatically convert j1 into j2?
Rashid Ali
Frequent Advisor

Re: How to grep the correct pattern?

Here is j2. Because one message can only have one attachment file.
Wieslaw Krajewski
Honored Contributor

Re: How to grep the correct pattern?

Hi,

In j1 file after 3.15 you have wheras in j2 there are simply spaces.
Can check this just by

grep "3.15" j1

grep "3.15" j2

where corresponds to pressing the Tab key.

As concerns convertion one file into another try sed or awk vommands.

Good Luck
Permanent training makes master
Rashid Ali
Frequent Advisor

Re: How to grep the correct pattern?

It seems using sed 's/\009/\020/g' j1>j3 to convert tab into space cannot work. How should I issue the command?
Wieslaw Krajewski
Honored Contributor

Re: How to grep the correct pattern?

Hi,

So, try the following

tr "" " " < j1 > j3

where means pressing the Tab key and means pressing space key.

It works, I am sure.
Permanent training makes master
Tim D Fulford
Honored Contributor

Re: How to grep the correct pattern?

Hi.
I've got to say I could not repeat the functionality of your script. However, if I understand the problem it is in the line
grep $ipaddr /tmp/ipaddr.txt
try
grep -x $ipaddr /tmp/ipaddr.txt
if you have a file /tmp/file.txt
15
150
grep 15 /tmp/file.txt
15
150
grep -x 15 /tmp/file.txt
15
-
Tim D Fulford
Honored Contributor

Re: How to grep the correct pattern?

I think I have got the script you want
#!/usr/bin/ksh
netstat -n | grep "^tcp" | awk '$4~/158/ || $5~/158/ {print $5}' | sed "s/\.[0-9][0-9]*$//" | sort -u > /tmp/tmpipaddr.lst

for ipaddr in $(cat /tmp/tmpipaddr.lst)
do
grep -x $ipaddr /tmp/ipaddr.txt
done

I changed the grep to an awk statement in the first line and got rid of the echo $(...) as it just issues a balnk line for each null find.

I hope this helps and reduces the scripts complexity.

Tim
-
Rashid Ali
Frequent Advisor

Re: How to grep the correct pattern?

grep -x is definitely not applicable to my case here since there is other user info besides ip address in ipaddr.txt.
Wieslaw's solution is working fine but I was wondering whether I can express it in binary format such as sed 's/\009/\020/g' j1>j3 or
tr "\009" "\020" j3, so that I don't need to press or . Anybody knows how to do that?