A help in regexp and grep

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 .

Thanks
Kumarjit.

You need to escape the '+', not the pipe character:

echo $str | grep -o -e '|\+'

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:

$ echo $str | grep -Eo  "\|+"
|

@Rudi, the double quotes or single quotes do not make a difference in this case:

$ echo "$str" |  grep -Eo '\|+'
|
1 Like

Rats! You're right. Writing a thesis and then the basics are wrong... still it needs the -E

@Subbeh : Tried echo $str | grep -o -e '|\+' , works good , but fails while using the following command
echo $str | grep -o -e '|\*'

@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)

How can I use the same logic with "*"

something like :: echo $str | grep -o -e '|\*' ......
But this isnt working.

---------- Post updated at 08:11 AM ---------- Previous update was at 08:10 AM ----------

How can I use the same logic with "*"

something like :: echo $str | grep -o -e '|\*' ......
But this isnt working.

Thanks again.

---------- Post updated 02-28-13 at 01:46 AM ---------- Previous update was 02-27-13 at 08:11 AM ----------

But the following code does not work : :frowning:

echo $str | grep -Eo "\|*"

I even tried

echo $str | grep -o -e "|\"
and
echo $str | grep -o -e "|\
"

A little help please.

Thanks
Kumarjit.

What is your OS and version? What does not work? What results do you get?