LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Why this grep command with regular expressions not working on my system? (http://www.linuxquestions.org/questions/linux-newbie-8/why-this-grep-command-with-regular-expressions-not-working-on-my-system-758692/)

Andrew Dufresne 09-30-2009 10:00 AM

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.

Question 3
I have used
Quote:

grep -o ^[[:alnum:]]* test.txt
grep -Eo ^[[:alnum:]]+ test.txt
and this time I got
Quote:

odsdsdoddf112
dad23392eeedJ
Hello
garbage
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?

Any ideas or suggestions are welcome.

Regards

SharpyWarpy 09-30-2009 10:24 AM

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).

Andrew Dufresne 09-30-2009 10:43 AM

Thanks SharpyWarpy for responding to my post.

Quote:

Originally Posted by SharpyWarpy (Post 3701922)
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 (Post 3701922)
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
$

SharpyWarpy 09-30-2009 06:31 PM

I meant a backward slash, \ , not a forward slash.
\grep ....
Very sorry for that.

chrism01 09-30-2009 06:55 PM

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.

Andrew Dufresne 09-30-2009 09:04 PM

Thanks guys for replying.

Quote:

Originally Posted by SharpyWarpy (Post 3702521)
I meant a backward slash, \ , not a forward slash.
\grep ....
Very sorry for that.

No need to apologize, no big deal.

Quote:

Originally Posted by chrism01 (Post 3702548)
The \ is to try and escape any aliases etc; you could try just giving the absolute path eg /usr/bin/grep

Here is the output :

Quote:

$ /bin/grep -Eo "^[[:alnum:]]+[[:blank:]]" test.txt
odsdsdoddf112
dad23392eeedJ
test2
for
Hello
World
$ \grep -Eo "^[[:alnum:]]+[[:blank:]]" test.txt
odsdsdoddf112
dad23392eeedJ
test2
for
Hello
World
$ grep -Eo "^[[:alnum:]]+[[:blank:]]" test.txt
odsdsdoddf112
dad23392eeedJ
test2
for
Hello
World
$
As you can see, in every case, result is same and incorrect.

Well, what to do now?

Kenhelm 09-30-2009 09:32 PM

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.


pixellany 09-30-2009 10:01 PM

Here is what I get:

Code:

[mherring@Ath ~]$ grep -Eo "^[[:alnum:]]+[[:blank:]]" file
odsdsdoddf112
dad23392eeedJ
Hello
garbage

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.

Andrew Dufresne 09-30-2009 10:04 PM

Quote:

Originally Posted by Kenhelm (Post 3702715)
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.

SharpyWarpy 09-30-2009 10:52 PM

To OP: Where did you get grep-2.5.4 ? I need it for my fc10 too. Thanks.

Andrew Dufresne 09-30-2009 10:57 PM

Quote:

Originally Posted by SharpyWarpy (Post 3702767)
To OP: Where did you get grep-2.5.4 ? I need it for my fc10 too. Thanks.

This is the link to GNU ftp mirror site grep section

http://www.promotionalpromos.com/mirrors/gnu/gnu/grep/

And this the file which I downloaded

http://www.promotionalpromos.com/mir...p-2.5.4.tar.gz

Regards

SharpyWarpy 09-30-2009 11:29 PM

Thanks bunches to one and all.

pixellany 10-01-2009 02:38 PM

wow!!! Here I am with RHEL 5.3----supposedly super-stable, conservative, etc.---but it has GREP v.2.5.1--bugs and all.

And, because it's RHEL, I can't do "pacman -Syu" :mad:


All times are GMT -5. The time now is 04:07 AM.