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. |
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 Code:
^ beginning of line (using -x it has no any meaning) |
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"] |
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.
|
All times are GMT -5. The time now is 03:50 PM. |