Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get egrep to match ^ for every line (as it should be)
    primarykey
    data
    text
    <p>I have a file containing these lines:</p> <pre><code>SOME COMMAND 34 XXXXX ; ; a comment which may contain a : sometext001 : X00 : 1 ; : X01 : 1 ; : X11 : 1 ; </code></pre> <p>And want to retrieve <code>sometext001</code> with grep/egrep.</p> <p>Using the regex <code>^\s*[^:\s;]+\s*:</code> </p> <p>(in words: starting at the beginning of the line with some or none whitespace, followed by at least one character not a whitespace, colon or semicolon followed again by some or none whitespaces followed by a colon)</p> <p>I'm able to match the text (including the following :) using an online regex tester <a href="http://regexr.com?35eam" rel="nofollow">http://regexr.com?35eam</a> if I enable multiline support.</p> <p>I was under the impression that grep/egrep works line by line anyway, so why does the regex not work when used with egrep on a file containing this example?</p> <p>Is there another way to achive the desired result with egrep or, if that's not possible, with another one-liner callable from a shell script?</p> <p><strong>Update:</strong> although the proposed change of the regex to <code>^[[:space:]]*[^[:space:];]+[[:space:]]*:</code> matches the lines specified, it it still matches twice in that line, once for <code>sometext001 :</code> and once for <code>X00 :</code> as evident when using the -o option to egrep. How to solve this?</p> <p><strong>Update:</strong> The test file contained exactly the text given above. The command line was <code>egrep -o '^([[:space:]]*[^:[:space:];]+[[:space:]]*:)' test.txt</code> (also tried without the () pair). Output is</p> <pre><code> sometext001 : X00 : </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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