Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-20-2015, 10:02 AM
|
#1
|
Member
Registered: Dec 2014
Posts: 56
Rep:
|
grepping a sentence?
hey guys, basic grep commands would be
#grep -r --col ping ./
or
#grep -i -r --col "sentence to search for" ./
but the sentence has to be exact. is there anyway to grep a sentence that contains those particular strings?
Example Document:
"a server has reported that ping has given code a222"
I can #grep -i -r --col "a server has reported" ./ and it will come out. but if I mix it like this,
#grep -i -r --col "server ping code" ./ it wont work and it will not return anything. Is there anyways to search for a sentence that "might contain some of those strings"? or would I have to use egrep and just break it up?
Last edited by willc86; 07-20-2015 at 10:34 AM.
|
|
|
07-20-2015, 10:30 AM
|
#2
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
The quotes should work but be aware this is case sensitive.
If your "when" is actually "When" then it wouldn't find it unless you specified "When..." in your grep OR if you specify the -i option (case insensitivity).
|
|
|
07-20-2015, 10:44 AM
|
#3
|
Member
Registered: Dec 2014
Posts: 56
Original Poster
Rep:
|
Quote:
Originally Posted by MensaWater
The quotes should work but be aware this is case sensitive.
If your "when" is actually "When" then it wouldn't find it unless you specified "When..." in your grep OR if you specify the -i option (case insensitivity).
|
correct, that is why I am using "-i" tag. However, i know i works with the sentence, but what I am trying to say is, the sentence has to be exact. there can not be no mismatch. example would be
"server has failed due to the chip 288"
I can # grep -i -r --col "server has failed" ./ and it will return something, but if I were to
# grep -i -r --col "failed chip 288" ./ it wont return anything because the sentence is not exact match. I was just wondering if there is a way to throw in a sentence and just find strings
or if I had a misspelled word it wont find it neither such as grep -i -r "serv failed chip 288" ./ ( i want it to return failed chip 288 rather than not retunring anything =P )
within that document without it having to have an exact match. I know you can use egrep, but its pretty annoying using the | in between strings.
Last edited by willc86; 07-20-2015 at 10:46 AM.
|
|
|
07-20-2015, 12:11 PM
|
#4
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
egrep (or grep -E) allows for or with the pipe sign as you have but doesn't have an "and" character.
However you can emulate "and" by using .* in your search:
egrep -i -r --col "sentence.*to.*search.*for" ./
The . matches any single character.
|
|
1 members found this post helpful.
|
07-20-2015, 12:12 PM
|
#5
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,021
|
Well as you might expect, if you misspell a word I do not believe anything can help you. The computer is not going to spell check prior to running your regex.
As for a sentence containing certain words, yes it is easily done with a regex, however, you must first consider just how mixed up will the words be that you use.
For example, based on the sentence ... "server has failed due to the chip 288", if your code were to use any of those words out of sequence then a single regex cannot help you.
So try googling for regular expressions and see how you go
|
|
|
All times are GMT -5. The time now is 10:30 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|