I have test string value , something like the one below
str='KUAMRJIT|GHOSH'
If I type
echo $str | grep -o -e '\|+'
[/COLOR]it doesnt give me anything .
But on the contrary
echo $str | grep -o -e '|'
display the only one pipe character(|) thats there in the string above .
The way I understood Unix regexp and grep command is
+ : matches one / more occurence of the preceeding character
-o : displays all the occurences of patter in the file / standard input
So why the first pattern yeilds nothing but the second does ?
Does grep -o option match literal values only ?
Please help .
Single quoting supersedes escaping, so it is looking for \|+ or |\+ verbatim. Put the expression in double quotes. Interpreting the + needs extended regular expressions ("ERE"), so use grep -E . As | is the branch separator in ERE, you need to escape it. The -e option is dispensabe. So the following will work:
@kumarjt, Subbeh, that would only work with GNU grep (and then -e would be unnecessary and \+ would be a GNU extension on Basic Regular Expression and * would not need a \ )
IMO it is better to use RudiC's suggestion and use the -E option (Extended Regular Expression)