Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make grep stop at first match on a line?
    text
    copied!<p>Well, I have a file test.txt</p> <pre> #test.txt odsdsdoddf112 test1_for_grep dad23392eeedJ test2 for grep Hello World test garbage </pre> <p>I want to extract strings which have got a space after them. I used following expression and it worked</p> <pre> grep -o [[:alnum:]]*.[[:blank:]] test.txt </pre> <p>Its output is </p> <pre> odsdsdoddf112 dad23392eeedJ test2 for Hello World </pre> <p>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.</p> <p>Which expression should I use here, in order to make it stop after first match and move to next line?</p> <p>This problem may be solved with gawk or some other tool, but I will appreciate a solution which uses grep only.</p> <p><strong>Edit</strong> I using GNU grep 2.5.1 on a Linux system, if that is relevant. </p> <p><strong>Edit</strong></p> <p>With the help of the answers given below, I tried my luck with </p> <pre> grep -o ^[[:alnum:]]* test.txt grep -Eo ^[[:alnum:]]+ test.txt </pre> <p>and both gave me correct answers. </p> <p>Now what surprises me is that I tried using </p> <pre> grep -Eo "^[[:alnum:]]+[[:blank:]]" test.txt </pre> <p>as suggested <a href="https://stackoverflow.com/questions/1495229/how-to-make-grep-stop-at-first-match-on-a-line/1495253#1495253">here</a> but didn't get the correct answer. Here is the output on my terminal</p> <pre> odsdsdoddf112 dad23392eeedJ test2 for Hello World </pre> <p>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.</p> <p><strong>Edit</strong></p> <p>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 <a href="http://www.linuxquestions.org/questions/linux-newbie-8/why-this-grep-command-with-regular-expressions-not-working-on-my-system-758692/" rel="nofollow noreferrer">this link</a> for details.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload