LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 01-13-2016, 08:51 AM   #1
amanbijpuria
LQ Newbie
 
Registered: Jan 2016
Posts: 4

Rep: Reputation: Disabled
grep command with regular expression


I have a file name abc.txt which contain lines

lightdm:x:112:118:Light DisplayyDiManager:/var/lib/lightdm:/bin/false
dnsmasq:x:104:655:dnsmasq,,,:/var/lib/misc:/bin/false


when i do grep -i 'display*' abc.txt it is giving me output i.e 1st line
but when i do grep -i 'display+' abc.txt it gives nothing but it should give .i dont know why. please let me know if anyone knows. thanks in advance

Last edited by amanbijpuria; 01-13-2016 at 11:01 AM.
 
Old 01-13-2016, 09:22 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 22,307

Rep: Reputation: 6008Reputation: 6008Reputation: 6008Reputation: 6008Reputation: 6008Reputation: 6008Reputation: 6008Reputation: 6008Reputation: 6008Reputation: 6008Reputation: 6008
Quote:
Originally Posted by amanbijpuria View Post
I have a file name abc.txt which contain lines

lightdm:x:112:118:Light DisplayyDiManager:/var/lib/lightdm:/bin/false
dnsmasq:x:104:655:dnsmasq,,,:/var/lib/misc:/bin/false


when i do grep -i 'display*' abc.txt it is giving me output i.e 1st line but when i do grep -i 'display*' abc.txt it gives nothing but it should give .i dont know why. please let me know if anyone knows. thanks in advance
Your post makes no sense...you posted the same command twice, saying the first time you run it it WORKS, then it DOESN'T...they both can't be true. Also, in the context you're using it, you don't need the wildcard, since grep will find the word display, regardless of what's before or after it.
 
1 members found this post helpful.
Old 01-13-2016, 11:05 AM   #3
amanbijpuria
LQ Newbie
 
Registered: Jan 2016
Posts: 4

Original Poster
Rep: Reputation: Disabled
first of all sorry for writing same code again ,i have corrected it.So instead of grep -i 'display*' abc.txt in 2nd line it will be grep -i 'display+' abc.txt .
 
Old 01-13-2016, 11:58 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,360

Rep: Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001
In basic regular expressions the "+" character is not special, so you are looking for that character to appear literally in the line. To use its special meaning, you would have to preceed it with a backslash:
Code:
grep -i 'display\+' abc.txt
But again, why are you doing that? That looks for the string "displa" followed by one or more repetitions of the character "y". Just plain "display" would also match that.
 
1 members found this post helpful.
Old 01-13-2016, 12:08 PM   #5
amanbijpuria
LQ Newbie
 
Registered: Jan 2016
Posts: 4

Original Poster
Rep: Reputation: Disabled
Hi Rknicholes,
Thankyou for your reply .I have just given an example where my file data is like that. Now my question here grep -i 'display*' abc.txt is working without any backslash but in case of grep -i 'display\+' abc.txt i have to use backslash .why?
 
Old 01-13-2016, 12:17 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,768

Rep: Reputation: 3051Reputation: 3051Reputation: 3051Reputation: 3051Reputation: 3051Reputation: 3051Reputation: 3051Reputation: 3051Reputation: 3051Reputation: 3051Reputation: 3051
You need to pay closer attention. The asterisk will work in a basic regular expression for grep but the plus is considered an extended expression so you either need to escape it to let grep
know it is special or use the -E switch to use extended expressions.

As pointed out, neither seems really relevant, so your actual examples must need this as the current one it seems superfluous.
 
1 members found this post helpful.
Old 01-13-2016, 12:24 PM   #7
amanbijpuria
LQ Newbie
 
Registered: Jan 2016
Posts: 4

Original Poster
Rep: Reputation: Disabled
Hi Grail,
Thanks a lot for your reply Grail.I think this information is very useful .
 
  


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
[SOLVED] How to use regular expression in grep? sagarkha Linux - Newbie 6 01-23-2010 09:15 AM
grep regular expression casperdaghost Linux - Newbie 2 08-15-2009 12:01 PM
grep Regular Expression lamar_air Programming 2 07-24-2006 04:40 PM
Help with grep regular expression lamar_air Programming 3 07-21-2006 12:26 PM
grep usage with regular expression jonathanztaub Linux - General 7 09-01-2004 11:35 PM

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

All times are GMT -5. The time now is 12:17 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration