简体   繁体   中英

grep those matching lines which contain single quotes

I want to matches those line of words:

{'ID': 'X.id: 4243.4, Y.id 534534', 'text': 'text - text'}
{'ID': 'X.id: 4247.4, Y.id 534534', 'text': 'text - text'}
{'ID': 'X.id: 42233.4, Y.id 534534', 'text': 'text - text'}

and I use this regular expression to match them:

{'ID': 'X.id: [0-9]+.[0-9], Y.id [0-9]+', 'text': 'text - text'}

I tested it on https://regexr.com/ and it works.

However I tried to used this with grep using this command:

cat raport.log | grep "{'ID': 'X.id: [0-9]+.[0-9], Y.id [0-9]+', 'text': 'text - text'}"

Where do I do the mistake?

You may either use the ERE POSIX pattern by adding the -E option or change the + quantified subpatterns to their equivalents with * (eg [0-9]+ => [0-9][0-9]* ) (or escape + in GNU grep ).

s="{'ID': 'X.id: 4243.4, Y.id 534534', 'text': 'text - text'}"
echo "$s" | grep -E "{'ID': 'X\.id: [0-9]+\.[0-9], Y\.id [0-9]+', 'text': 'text - text'}"
echo "$s" | grep "{'ID': 'X\.id: [0-9][0-9]*\.[0-9], Y\.id [0-9][0-9]*', 'text': 'text - text'}"

See an online demo

Note that you need to escape the dots to match literal . chars.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM