LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 10-08-2014, 09:30 AM   #1
barnabiofskies
LQ Newbie
 
Registered: Oct 2014
Posts: 2

Rep: Reputation: Disabled
Why grepping with regex is not working?


I'm fairly new to Linux. I've really been only doing some basic things and that's why I'm not sure exactly what is wrong with the approach I'm trying.

I need to grep a file and return the lines that are exactly 111 characters long. I've tested the regular expression "^.{111}?" here http://regex101.com/#pcre and it appears to be correct. However, when I try to grep the file in my Linux server it doesn't return any results. If I try something like:

egrep -x "^.*?" <file_name>

it returns all the lines in the file as you would expect, so I know that it is running the regex matching, while:

egrep -x "^.{111}?" <file_name>

returns nothing. I guess my question is, what am I doing wrong? Is it because my Linux version doesn't support certain regex characters (quantifiers)? Am I missing arguments in the command? Is my regular expression wrong? Does egrep or grep commands not work like that? Is there a non-regex way to do this with the grep command?

I'm running a Red Hat Enterprise Distribution currently. Any help or insight is greatly appreciated.
 
Old 10-08-2014, 09:41 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
that is not related to "your linux version". What do you think what does egrep -x "^.{111}?" <file_name> will do?
Code:
egrep: grep with extended regexp functionality
-x: Select only those matches that exactly match the whole line.
"^.{111}?" the search pattern
<filename>
And what is this search pattern:
Code:
^  beginning of line (using -x it has no any meaning)
.  any char
{111} exactly 111 pieces of that any char.
?     0 or 1 occurence of the previous thing - that is 111 of anything - or nothing
Oh yes, grep works always, or almost always - you will hardly find any bug in it.

Last edited by pan64; 10-08-2014 at 09:42 AM.
 
1 members found this post helpful.
Old 10-08-2014, 10:27 AM   #3
barnabiofskies
LQ Newbie
 
Registered: Oct 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks,

I though the ? at the end marked the end of the string, so that the expression evaluated to exactly 111 instances of any character between the beginning and end of the string.

I may have posted a little prematurely. The grep command with the regular expression was working. The problem was that the lines of text in the file had an added space character at the end, therefore the strings really consisted of 112 characters (I could have sworn I tried different amount of characters in this range to account for this possibility with no success, I must have just been tired and messed it up). Once I tried: egrep -x "^.{112}?" <file_name> the command worked the way I wanted it. My suggestion with this experience is if anyone has this problem to try different value ranges {100-250} and continue to refine them until you get the result you want.

Good to know that I don't need to worry about this with the distro I'm running and grep is reliable that way. Let's me cross possibilities off my checklist next time I need to troubleshoot something similar. I'll remove the -x argument also. Thanks again.[COLOR="Silver"]

Last edited by barnabiofskies; 10-08-2014 at 11:03 AM.
 
Old 10-08-2014, 10:30 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
you would need to use $ instead of ? (that is the end of line). Another tip can be to use online regexp tester, that also may help you to understand that expression.
 
1 members found this post helpful.
  


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
Confusing issue with Perl regEx - Regex check seems to require variable being set EnderX Programming 1 09-07-2013 05:36 AM
[SOLVED] RegEx is not working. sysmicuser Linux - Newbie 9 02-16-2012 09:21 AM
[SOLVED] awk computed regex not working as expected grail Programming 12 06-01-2011 12:16 PM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 02:10 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 09:08 PM


All times are GMT -5. The time now is 04:15 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