- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: how to '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
08-19-2007 05:34 PM
08-19-2007 05:34 PM
Assume below 'testfile' contains (5 lines),
> cat testfile
apple
apple1
apple2
apple3
apple4
apple5
Qn: Is there anyways I can "grep" only the word "apple". Like,
# grep -? "apple" testfile
I need the output only as "apple".
--WH
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2007 06:04 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2007 06:15 PM
08-19-2007 06:15 PM
Re: how to 'grep' !!
see man grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2007 06:20 PM
08-19-2007 06:20 PM
Re: how to 'grep' !!
because a string like "apple" might not be the last string of a record in a file one likely more often has some following fields separated by whitespace or some other separator.
In such a case you could use the extended regexp engine of grep and use something similar to this
$ grep -E 'apple[^[:alnum:]]+'
grep -E or egrep can cope with much more intricate filters (see man regexp).
Albeit, the scope of grep is a bit limited and sometimes one needs a sharper tool like Perl which deserved much of its fame from its intrinsic regular expression capabilities.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2007 07:20 PM
08-19-2007 07:20 PM
Re: how to 'grep' !!
Qn2: If my file contains,
>cat testfile
one
two
three
four
five
How can I grep the words "one" "three" and "four" using a single "grep" command, like,
#grep -? -? "one" -? "three" -? "four"
--WH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2007 07:24 PM
08-19-2007 07:24 PM
Re: how to 'grep' !!
$ cat yourfile | grep -E "one|three|four"
see also man regexp
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2007 07:27 PM
08-19-2007 07:27 PM
Re: how to 'grep' !!
cat file| grep -e expr1 -e expr2
grep -e "one" -e "three" -e "four"
regards,
ivan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2007 07:30 PM
08-19-2007 07:30 PM
Re: how to 'grep' !!
for i in one three four
do
grep $i testfile
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2007 10:37 PM
08-19-2007 10:37 PM
Re: how to 'grep' !!
You can just check this command
cat testfile | grep -i apple
"i" makes it case insensitive.
Thanx
Rahul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2007 11:22 PM
08-19-2007 11:22 PM
Re: how to 'grep' !!
Contrary to what Rahul, Oviwan and Ivan used, you don't have to use cat with grep. grep takes a list of files.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2007 12:53 AM
08-20-2007 12:53 AM
Re: how to 'grep' !!
The grep -w option tends to work great, if your grep supports it at all.
Personally I mostly use perl to do my greps and more.
Perl as a "\b" escape for "boundary"
Example:
$ cat > x
apple
apple1
apple2
apple3
snapple
an apple a day
quoted 'apple' test
double-quoted "apple" test
$ perl -ne "print if /\bapple\b/" x
apple
an apple a day
quoted 'apple' test
double-quoted "apple" test
Enjoy.
Hein.