- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: grep and whole words
Operating System - Linux
1753616
Members
6014
Online
108797
Solutions
Forums
Categories
Company
Local Language
юдл
back
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
юдл
back
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
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- 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
тАО12-23-2002 02:07 AM
тАО12-23-2002 02:07 AM
Hello:
imagine the next file
---------
/test
/test/hello
/testA
---------
I need to make a grep over it to obtain only the first entry(/test). If I use the -w option I get the following
/test
/test/hello
This must be easy but I don??t know how to do it.
Regards.
imagine the next file
---------
/test
/test/hello
/testA
---------
I need to make a grep over it to obtain only the first entry(/test). If I use the -w option I get the following
/test
/test/hello
This must be easy but I don??t know how to do it.
Regards.
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-23-2002 05:56 AM
тАО12-23-2002 05:56 AM
Re: grep and whole words
you can pipe it to head
like
/tmp/a
---------
/test
/test/hello
/testA
----------
cat /tmp/a|grep test |head -1
like
/tmp/a
---------
/test
/test/hello
/testA
----------
cat /tmp/a|grep test |head -1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-23-2002 08:21 AM
тАО12-23-2002 08:21 AM
Re: grep and whole words
With what you've shown here you can get the results you want with an end-of-line metacharacter. Your grep command would look like
grep "/test$" myfile
Hope this helps,
ard
grep "/test$" myfile
Hope this helps,
ard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-24-2002 06:37 AM
тАО12-24-2002 06:37 AM
Solution
If you really want to worry about "words" and assume that the word delimeter is white space (tab or blank), then a more complete expression would be:
grep -E '/test$|/test[[:space:]]' /tmp/x
This checks for the word "/test" at the end of line *or* followed by a whitespace char.
E.g., for the file:
# cat /tmp/x
/test
/test one blank
/test 2 blanks
/test tab
/test/hello
/testA
the results are:
# grep -E '/test[[:space:]]|/test$' /tmp/x
/test
/test one blank
/test 2 blanks
/test tab
bv
grep -E '/test$|/test[[:space:]]' /tmp/x
This checks for the word "/test" at the end of line *or* followed by a whitespace char.
E.g., for the file:
# cat /tmp/x
/test
/test one blank
/test 2 blanks
/test tab
/test/hello
/testA
the results are:
# grep -E '/test[[:space:]]|/test$' /tmp/x
/test
/test one blank
/test 2 blanks
/test tab
bv
"The lyf so short, the craft so long to lerne." - Chaucer
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP