Ubuntu – Problems with “+” in grep

command linegrepregex

I'm trying to write a grep command to find lines like the below in a large text file:

<div class="node_thumbnail" data-type="file" name="GOPR0036.MP4_frame000001.jpg" data="813334c25191468c9f1c57afc99fde60" aid="133948" rel="/Files/ToolTipView?fileId=813334c25191468c9f1c57afc99fde60&pageNo=1&NoCache=101016083044" rev="topMiddle">

but the + symbol seems to be causing problems in the below commands:

 grep 'data=[a-z,0-9,\"]' file

Lots of hits

 grep 'data=[a-z,0-9,\"]+' file

No hits

Best Answer

If you want + to mean "one or more of the preceding atom", then you have to do one of:

  1. Use -E (Extended Regular Expressions) (or -P, PCRE):

    grep -E 'data=[a-z,0-9,\"]+' file
    
  2. Escape + so that is treated specially in the Basic Regular Expressions used by default in grep:

    grep 'data=[a-z,0-9,"]\+' file
    
Related Question