Grep does not show results, online regex tester does -
i unexperienced behavior of grep. have bunch of xml files contain lines these:
<identifier type="abc">abc:def.ghi/g1234.ab012345</identifier> <identifier type="abc">abc:def.ghi/g5678m.ab678901</identifier>
i wanted identifier part after slash , constructed regex using regexpal:
[a-z]\d{4}[a-z]*\.[a-z]*\d*
it highlights wanted. perfect. when run grep on same file, don't results. , said, don't know grep, tried different combinations.
grep [a-z]\d{4}[a-z]*\.[a-z]*\d* test.xml grep "[a-z]\d{4}[a-z]*\.[a-z]*\d*" test.xml egrep "[a-z]\d{4}[a-z]*\.[a-z]*\d*" test.xml grep '[a-z]\d{4}[a-z]*\.[a-z]*\d*' test.xml grep -e '[a-z]\d{4}[a-z]*\.[a-z]*\d*' test.xml
what doing wrong?
your regex doesn't match input. let's break down:
[a-z]
matchesg
\d{4}
matches1234
[a-z]*
doesn't match5
also, believe grep
, family don't \d
syntax. try either [0-9]
or [:digit:]
finally, when using regular expressions, prefer egrep
grep
. don't remember exact details, egrep
supports more regex operators. also, in many shells (including bash on os x mentioned, use single quotes instead of double quotes, otherwise *
expanded shell list of files in current directory before grep sees (and other shell meta-characters expanded too). bash won't touch in single quotes.
Comments
Post a Comment