LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 09-30-2009, 10:00 AM   #1
Andrew Dufresne
LQ Newbie
 
Registered: Sep 2009
Distribution: Fedroa 10
Posts: 27

Rep: Reputation: 16
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
 
Old 09-30-2009, 10:24 AM   #2
SharpyWarpy
Member
 
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862

Rep: Reputation: 91
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).
 
Old 09-30-2009, 10:43 AM   #3
Andrew Dufresne
LQ Newbie
 
Registered: Sep 2009
Distribution: Fedroa 10
Posts: 27

Original Poster
Rep: Reputation: 16
Thanks SharpyWarpy for responding to my post.

Quote:
Originally Posted by SharpyWarpy View Post
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 View Post
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
$
 
Old 09-30-2009, 06:31 PM   #4
SharpyWarpy
Member
 
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862

Rep: Reputation: 91
I meant a backward slash, \ , not a forward slash.
\grep ....
Very sorry for that.
 
Old 09-30-2009, 06:55 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
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.
 
Old 09-30-2009, 09:04 PM   #6
Andrew Dufresne
LQ Newbie
 
Registered: Sep 2009
Distribution: Fedroa 10
Posts: 27

Original Poster
Rep: Reputation: 16
Thanks guys for replying.

Quote:
Originally Posted by SharpyWarpy View Post
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 View Post
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?
 
Old 09-30-2009, 09:32 PM   #7
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
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.
 
Old 09-30-2009, 10:01 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 09-30-2009, 10:04 PM   #9
Andrew Dufresne
LQ Newbie
 
Registered: Sep 2009
Distribution: Fedroa 10
Posts: 27

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by Kenhelm View Post
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.
 
Old 09-30-2009, 10:52 PM   #10
SharpyWarpy
Member
 
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862

Rep: Reputation: 91
To OP: Where did you get grep-2.5.4 ? I need it for my fc10 too. Thanks.
 
Old 09-30-2009, 10:57 PM   #11
Andrew Dufresne
LQ Newbie
 
Registered: Sep 2009
Distribution: Fedroa 10
Posts: 27

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by SharpyWarpy View Post
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
 
Old 09-30-2009, 11:29 PM   #12
SharpyWarpy
Member
 
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862

Rep: Reputation: 91
Thanks bunches to one and all.
 
Old 10-01-2009, 02:38 PM   #13
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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"
 
  


Reply

Tags
grep



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
How do I make a command accept regular expressions AwesomeMachine Linux - Newbie 3 06-01-2007 09:09 AM
Regular Expression + not Working with grep scottwmackey Linux - General 2 06-21-2006 01:31 AM
Regular expressions bhuwan Programming 5 02-25-2006 11:07 PM
Regular expressions using grep linuxmandrake Programming 3 11-16-2005 04:29 PM

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

All times are GMT -5. The time now is 12:24 AM.

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
Open Source Consulting | Domain Registration