- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Help with Grep
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
02-14-2001 11:51 AM
02-14-2001 11:51 AM
I am doing a grep on a file. I only want lines where the search string is matched exactly.
command % grep exam filename
filename contents:
line 1)here:is:my:exam:contents
line 2)here:is:yourproblem:example
The problem is that grep will return both lines because it matches all or part of the search string. Lines that conatina both "exam" and "example" are returned. I only want line 1 to be returned, an exact match. I have looked thru the man pages and I don't see any options that will work. I know it can be done. I am also looking into awk. Any ideas????
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2001 11:56 AM
02-14-2001 11:56 AM
Re: Help with Grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2001 11:58 AM
02-14-2001 11:58 AM
Re: Help with Grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2001 12:28 PM
02-14-2001 12:28 PM
Re: Help with Grep
Check the man page for grep.
The -x option returns only exact matches.
Tony
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2001 12:30 PM
02-14-2001 12:30 PM
Re: Help with Grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2001 01:22 PM
02-14-2001 01:22 PM
Re: Help with Grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2001 03:03 PM
02-14-2001 03:03 PM
Solutionawk -F: '$3="exam"{print $0}'
It is more cryptic looking, but if you are planning on doing look ups in a script and need exact matches on a field within a file, then awk gives you more control.
By the way, the -x option of grep looks for exact matches across the entire line.
If you are planning on doing some really tricky stuff with the file, then "perl" has much better text file processing.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2001 01:15 AM
02-15-2001 01:15 AM
Re: Help with Grep
You'll have to use grep with a character class like in:
grep -E "[^[:alpha:]]exam[^[:alpha:]]"
This will match the string "exam" providing it is surrounded by non alpha characters.
If you want to match your string in cases where it is either starting or ending the line, then the expression becomes:
grep -E "[^[:alpha:]]exam[^[:alpha:]]|^exam[^[:alpha:]]|[^[:alpha:]]exam$"
Becomes to be unreadable, doesn't it ?
;-)
See man regexp for definitions of character classes.
Best regards,
Dan