- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Using Grep to find lines with pattern 1 AND pa...
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
Discussions
Discussions
Forums
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
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
тАО09-13-2010 04:53 PM
тАО09-13-2010 04:53 PM
Using Grep to find lines with pattern 1 AND pattern 2
I know you can use grep to find pattern 1 or pattern 2 etc... Can you use it to find lines that only contain 2 specfic patterns? I am searching a weblog for a specific IP address and a specific referring site starting with '?source=Ad'
So my 2 patterns would be the IP adress '1.1.1.1' and '?source=' and I only want lines that contain BOTH of these not either of them.
Thanks
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-13-2010 05:23 PM
тАО09-13-2010 05:23 PM
Re: Using Grep to find lines with pattern 1 AND pattern 2
grep 1.1.1.1 MyFile | grep '?source='
Not the most elegant, but complex awk code will be difficult to maintain.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-13-2010 06:31 PM
тАО09-13-2010 06:31 PM
Re: Using Grep to find lines with pattern 1 AND pattern 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-13-2010 06:34 PM
тАО09-13-2010 06:34 PM
Re: Using Grep to find lines with pattern 1 AND pattern 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-13-2010 11:42 PM
тАО09-13-2010 11:42 PM
Re: Using Grep to find lines with pattern 1 AND pattern 2
grep '1\.1\.1\.1.*?source='
And if you don't, you can put both orders:
grep -e '1\.1\.1\.1.*?source=' -e '?source=.*1\.1\.1\.1'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-14-2010 02:37 AM
тАО09-14-2010 02:37 AM
Re: Using Grep to find lines with pattern 1 AND pattern 2
I know you r looking for a solution with grep;
as alternatively you can use Perl:
perl -ne 'print if /1.1.1.1/|/\?source=/' Your_File
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-14-2010 04:10 AM
тАО09-14-2010 04:10 AM
Re: Using Grep to find lines with pattern 1 AND pattern 2
> Hakki: perl -ne 'print if /1.1.1.1/|/\?source=/'
No, this has two things very wrong. Joe only wants matches for the presence of BOTH strings on a line. Your regex specifies alternation ('|') or "or".
The second problem is that a dot character matches anything. Thus '/1.1.1.1/ will match '1a1b1c1d' just as well. This is why Dennis escaped the dot characters in his regular expression. Bill's 'grep' and Raj's 'awk' solution suffer this too.
We could do:
# perl -ne 'print if m{1\.1\.1\.1} && m{\?source=} file
...which meets Joe's needs. I chose alternate delimiters to avoid the "leaning toothpick" look.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-14-2010 11:23 PM
тАО09-14-2010 11:23 PM
Re: Using Grep to find lines with pattern 1 AND pattern 2
Thank you for correction, I was totally wrong :(
closing apostrophe not printed below :
# perl -ne 'print if m{1\.1\.1\.1} && m{\?source=} file
# perl -ne 'print if m{1\.1\.1\.1} && m{\?source=}' file