tags:

views:

505

answers:

2

Well, I have a file test.txt

#test.txt
odsdsdoddf112 test1_for_grep
dad23392eeedJ test2 for grep
Hello World test
garbage

I want to extract strings which have got a space after them. I used following expression and it worked

grep -o  [[:alnum:]]*.[[:blank:]]  test.txt

Its output is

odsdsdoddf112
dad23392eeedJ 
test2 
for    
Hello 
World

But problem is grep prints all the strings that have got space after them, where as I want it to stop after first match on a line and then proceed to second line.

Which expression should I use here, in order to make it stop after first match and move to next line?

This problem may be solved with gawk or some other tool, but I will appreciate a solution which uses grep only.

Edit I using GNU grep 2.5.1 on a Linux system, if that is relevant.

Edit

With the help of the answers given below, I tried my luck with

grep  -o   ^[[:alnum:]]*  test.txt
grep  -Eo  ^[[:alnum:]]+  test.txt

and both gave me correct answers.

Now what surprises me is that I tried using

grep -Eo "^[[:alnum:]]+[[:blank:]]" test.txt

as suggested here but didn't get the correct answer. Here is the output on my terminal

odsdsdoddf112
dad23392eeedJ 
test2 
for    
Hello 
World

But comments from RichieHindle and Adrian Pronk, shows that they got correct output on their systems. Anyone with some idea that why I too am not getting the same result on my system. Any idea? Any help will be appreciated.

Edit

Well, it seems that grep 2.5.1 has some bug because of which my output wasn't correct. I installed grep 2.5.4, now it is working correctly. Please see this link for details.

+3  A: 

If you're sure you have no leading whitespace, add a ^ to match only at the start of a line, and change the * to a + to match only when you have one or more alphanumeric characters. (That means adding -E to use extended regular expressions).

grep -Eo "^[[:alnum:]]+[[:blank:]]" test.txt

(I also removed the . from the middle; I'm not sure what that was doing there?)

RichieHindle
I copied your command to my terminal but got the exactly same result.
Andrew-Dufresne
@andrew-dufresne: Is there something weird about your text file? Try `od -c test.txt` Does that show the characters you were expecting to see?
RichieHindle
@RichieHindle : Thanks for your prompt responses, I really appreciate it. I executed the command and got the same characters as in the test.txt
Andrew-Dufresne
I tried this using GNU grep 2.5.3 and it produces the output I expected: odsdsdoddf112 <NEWLINE>dad23392eeedJ <NEWLINE>Hello <NEWLINE>
Adrian Pronk
@RichieHindle and @Adrian Pronk : This time I tried grep -o ^[[:alnum:]]* test.txt and it worked correctly. Thanks a lot.
Andrew-Dufresne
@RichieHindle and @Adrian Pronk : With grep 2.5.4, your expression worked. grep 2.5.1 has some bug in it. Thanks for your time and help
Andrew-Dufresne
+1  A: 
grep -oe "^[^ ]* " test.txt
BryanH
I pasted your command and got the same result as I have got earlier.
Andrew-Dufresne
Hmm - works for me with GNU Grep 2.5.3 (Ubuntu Hardy)
BryanH
You are right. My grep version 2.5.1 was the culprit. Installed 2.5.4 and your expression worked. Thanks a lot BryanH. I appreciate your help.
Andrew-Dufresne