Search code examples
bashgrepbrackets

How to use grep with o option and [ ]?


I am trying to figure how to use grep with -o and [ ] brackets. So i have a line that says

 <START connection="IP" name="test" extraname="testa" 2" data1="1" IP="192.1.1.1"></START>
 <START connection="IP" name="1test2" extraname="testb" 2" data1="1" IP="192.1.1.2"></START>

I basically want name="test 2"

so i tried:

grep -o 'name="[*]"'
grep -o 'name="*"'

I mostly get name="". Also a link to a page that shows lots of examples would be great also.


Solution

  • You probably need

    grep -o 'name="[^"]\+"' your_file
    

    Output:

    name="test"
    name="testa"
    name="1test2"
    name="testb"
    

    And this is equivalent:

    grep -Eo 'name="[^"]+"' your_file