- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to grep the correct pattern?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2001 07:29 PM
04-19-2001 07:29 PM
How to grep the correct pattern?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2001 07:36 PM
04-19-2001 07:36 PM
Re: How to grep the correct pattern?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2001 07:40 PM
04-19-2001 07:40 PM
Re: How to grep the correct pattern?
awk { print $5" " }
Then when you grep for $ipaddr" ", the space character will identify the end of significant data.
Hope this helps, FRED
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2001 09:45 PM
04-19-2001 09:45 PM
Re: How to grep the correct pattern?
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2001 10:13 PM
04-19-2001 10:13 PM
Re: How to grep the correct pattern?
while read ipaddr
do
echo $(grep "$ipaddr$" /tmp/ipaddr.txt)
done
federico
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2001 03:49 AM
04-20-2001 03:49 AM
Re: How to grep the correct pattern?
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 } ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2001 06:14 AM
04-20-2001 06:14 AM
Re: How to grep the correct pattern?
do
grep "$ipaddr\$" /tmp/ipaddr.txt
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2001 10:06 AM
04-20-2001 10:06 AM
Re: How to grep the correct pattern?
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2001 02:28 PM
04-20-2001 02:28 PM
Re: How to grep the correct pattern?
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2001 07:20 PM
04-20-2001 07:20 PM
Re: How to grep the correct pattern?
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2001 07:22 PM
04-20-2001 07:22 PM
Re: How to grep the correct pattern?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2001 07:13 AM
04-21-2001 07:13 AM
Re: How to grep the correct pattern?
In j1 file after 3.15 you have
Can check this just by
grep "3.15
grep "3.15
where
As concerns convertion one file into another try sed or awk vommands.
Good Luck
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2001 04:04 PM
04-22-2001 04:04 PM
Re: How to grep the correct pattern?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2001 10:39 PM
04-22-2001 10:39 PM
Re: How to grep the correct pattern?
So, try the following
tr "
where
It works, I am sure.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2001 12:42 AM
04-23-2001 12:42 AM
Re: How to grep the correct pattern?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2001 02:29 AM
04-23-2001 02:29 AM
Re: How to grep the correct pattern?
#!/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2001 05:34 PM
04-23-2001 05:34 PM
Re: How to grep the correct pattern?
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"