[SOLVED] Why this grep command with regular expressions not working on my system?
Linux - NewbieThis 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.
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.
Why this grep command with regular expressions not working on my system?
Hi,
Well, actually I have a problem with a text file, test.txt.
Quote:
#test.txt
odsdsdoddf112 test1_for_grep
dad23392eeedJ test2 for grep
Hello World test
garbage
I am trying to find all those words that have a space after them. Each line have more than one such words, but I only want the first one. In other words grep should stop at first match on a line and then proceed to next line.
Question 1
What will you suggest? How can I solve this problem? I only want to use grep and no other tool like gawk.
Question 2
I asked this question here and I was told to use
Quote:
grep -Eo "^[[:alnum:]]+[[:blank:]]" test.txt
But this didn't gave me correct result. My output is
Quote:
odsdsdoddf112
dad23392eeedJ
test2
for
Hello
World
Quite contrary, correct result will be
Quote:
odsdsdoddf112
dad23392eeedJ
Hello
People there say that they are getting correct result. But the expression is not working for me. I have tried it on two Linux systems and got the same wrong result.
Why is it so? Those regular expressions are working for them, but in my case it fails.
What could be the reason? Any idea or help will be greatly appreciated.
But clearly, last entry 'garbage' doesn't belong here because it doesn't have a space after it. Which expression should I add here to get correct result?
Enter the following command and post results:
alias
OR try this:
/
before your "grep" command. I'm thinking the grep command might be aliased with some conflicting option(s).
Enter the following command and post results:
alias
Here is the output
Quote:
$ alias grep
bash: alias: grep: not found
$
Quote:
Originally Posted by SharpyWarpy
OR try this:
/
before your "grep" command. I'm thinking the grep command might be aliased with some conflicting option(s).
I am not sure that I understand this point. Anyway, this is what I did
Quote:
$ /grep -Eo "^[[:alnum:]]+[[:blank:]]" test.txt
bash: /grep: No such file or directory
$ / grep -Eo "^[[:alnum:]]+[[:blank:]]" test.txt
bash: /: is a directory
$
The \ is to try and escape any aliases etc; you could try just giving the absolute path eg /usr/bin/grep
Unfortunately I don't have a Linux avail right now, and the Solaris grep doesn't recognise those options, so I can't help.
I get different output according to which version of grep I use.
Code:
lines='
odsdsdoddf112 test1_for_grep
dad23392eeedJ test2 for grep
Hello World test
garbage'
# Using grep version 2.5.1(PCLinuxOS2009.2 and Mandriva2006)
echo "$lines" | grep -Eo "^[[:alnum:]]+[[:blank:]]"
odsdsdoddf112
dad23392eeedJ
test2
for
Hello
World
# Using grep version 2.5.3(Puppy4.20) and 2.5.4(Mandriva2009.0)
echo "$lines" | grep -Eo "^[[:alnum:]]+[[:blank:]]"
odsdsdoddf112
dad23392eeedJ
Hello
It looks as though there is a bug in 'grep -o' version 2.5.1
The 'Beginning-of-line' which is matched by '^' seems to get placed after the last matched sub-string before the next match attempt. So '^' can end up matching positions in the middle of the line.
e.g. (using grep version 2.5.1)
Code:
echo "abcdefghi" | grep -o '^...'
abc
def
ghi # Output should have been only 'abc'
# It's not that '^' is just being ignored
echo "ab" | grep -o '^b'
# No output, '^' has worked.
This is what I would expect. The Regex says: "at the beginning of the line, match at least one alphanumeric character, followed by a blank. (blank means ( I think) a non-printing character, but not a line feed.
Once it matches the regex, it won't look at the rest of the line.
I get different output according to which version of It looks as though there is a bug in 'grep -o' version 2.5.1
The 'Beginning-of-line' which is matched by '^' seems to get placed after the last matched sub-string before the next match attempt. So '^' can end up matching positions in the middle of the line.
e.g. (using grep version 2.5.1)
Code:
echo "abcdefghi" | grep -o '^...'
abc
def
ghi # Output should have been only 'abc'
# It's not that '^' is just being ignored
echo "ab" | grep -o '^b'
# No output, '^' has worked.
Thanks a lot Kenhelm. The idea of a bug in GNU grep never crossed my mind.
I have installed grep 2.5.4 and this time it has worked.
Thanks to all of you for helping me.
Last edited by Andrew Dufresne; 09-30-2009 at 10:11 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.