Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make grep stop at first match on a line?

Tags:

grep

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.

like image 357
Andrew-Dufresne Avatar asked Sep 29 '09 22:09

Andrew-Dufresne


People also ask

How do you grep at the beginning of a line?

Use the ^ (caret) symbol to match expression at the start of a line. In the following example, the string kangaroo will match only if it occurs at the very beginning of a line. Use the $ (dollar) symbol to match expression at the end of a line.

How do I stop grep search?

End it by closing your quote (i.e. typing another apostrophe). Or, if you've changed your mind and you don't want to execute the command any more, ctrl c will get you out of the command and back into the shell. Save this answer.

How do I get my first grep match?

-m 1 means return the first match in any given file. But it will still continue to search in other files. Also, if there are two or more matched in the same line, all of them will be displayed.

How do you grep a line after a match?

Using the grep Command. If we use the option '-A1', grep will output the matched line and the line after it.


2 Answers

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

like image 83
RichieHindle Avatar answered Dec 25 '22 06:12

RichieHindle


As the questioner discovered, this is a bug in versions of GNU grep prior to 2.5.3. The bug allows a caret to match after the end of a previous match, not just at beginning of line.

This bug is still present in other versions of grep, for instance in Mac OS X 10.9.4.

There isn't a universal workaround, but in the some examples, like non-spaces followed by a space, you can often get the desired behavior by leaving off the delimiter. That is, search for '[^ ]*' rather than '[^ ]* '.

like image 45
Robert Tupelo-Schneck Avatar answered Dec 25 '22 08:12

Robert Tupelo-Schneck