LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 07-20-2015, 10:02 AM   #1
willc86
Member
 
Registered: Dec 2014
Posts: 56

Rep: Reputation: Disabled
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.
 
Old 07-20-2015, 10:30 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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).
 
Old 07-20-2015, 10:44 AM   #3
willc86
Member
 
Registered: Dec 2014
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MensaWater View Post
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.
 
Old 07-20-2015, 12:11 PM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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.
Old 07-20-2015, 12:12 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,021

Rep: Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
An iptable doubt, they are not the same sentence? loureed4 Linux - Security 9 07-26-2011 11:25 AM
why did KWrite highlight a sentence for me? newbiesforever Linux - Software 2 06-08-2009 09:03 AM
Dividing sentence into words TheMstrLaw Programming 5 11-02-2008 07:59 AM
What is bad in this sentence? Coimbra Linux - Newbie 9 07-11-2007 02:15 PM
What does this sentence mean? shadkong Slackware 9 04-07-2005 06:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration