Visit Our UNIX and Linux User Community


A help in regexp and grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A help in regexp and grep
# 1  
Old 02-27-2013
A help in regexp and grep

I have test string value , something like the one below
Code:
str='KUAMRJIT|GHOSH'

If I type
Code:
echo $str | grep -o -e '\|+'


it doesnt give me anything .
But on the contrary
Code:
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.

Last edited by Scrutinizer; 02-27-2013 at 07:29 AM.. Reason: code tags
# 2  
Old 02-27-2013
You need to escape the '+', not the pipe character:
Code:
echo $str | grep -o -e '|\+'

# 3  
Old 02-27-2013
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:
Code:
$ echo $str | grep -Eo  "\|+"
|

# 4  
Old 02-27-2013
@Rudi, the double quotes or single quotes do not make a difference in this case:
Code:
$ echo "$str" |  grep -Eo '\|+'
|

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 02-27-2013
Rats! You're right. Writing a thesis and then the basics are wrong... still it needs the -E
# 6  
Old 02-27-2013
@Subbeh : Tried echo $str | grep -o -e '|\+' , works good , but fails while using the following command
echo $str | grep -o -e '|\*'
# 7  
Old 02-27-2013
@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)

Previous Thread | Next Thread
Test Your Knowledge in Computers #670
Difficulty: Medium
Fourteen channels are designated in the 2.4 GHz range, spaced 5 MHz apart from each other except for a 12 MHz space before channel 14.
True or False?

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

A Regexp You Can Use Everywhere

\_(ツ)_/ bakunin (0 Replies)
Discussion started by: bakunin
0 Replies

2. Shell Programming and Scripting

Regexp

I would like to extract "1333 Fairlane" given the below text. The word "Building:" is always present. The wording between Building and the beginning of the address can be almost anything. It appears the the hyphen is there most of the time. Campus: Fairlane Business Park Building:... (9 Replies)
Discussion started by: bbaker@copesan.
9 Replies

3. Shell Programming and Scripting

Filter non-alpha character with grep/regexp

Hi all, I am trying to filter out those lines that contain a "non-alpha" character. An example of my input is the following: zygnematales grb zygocactus grb zygocactus_truncatus plt zygodactyl_foot prt zygoma prt zygomatic prt zygomatic_arch prt zygomatic_bone ... (2 Replies)
Discussion started by: owwow14
2 Replies

4. UNIX for Dummies Questions & Answers

Grep Regexp not working correctly

Consider the following code: grep -o -e '^STEAM_::\d+$' workfile3.tmp A sample format of a valid string for the regexp would be: STEAM_0:1:12345678 Here is an example line from the workfile3.tmp file: 465:L 01/02/2012 - 00:05:33: "Spartan1-1-7<8><STEAM_0:1:47539638><>" connected No... (2 Replies)
Discussion started by: spinner0205
2 Replies

5. Shell Programming and Scripting

help with grep regexp

My input file looks like this: 13154|X,the deer hunter 13154|Y,the good life 1316|,american idol 1316|,bowling 1316|,chuck etc... The X, Y, or any other character (besides a comma) after the pipe is a "Device Type". I want to strip out lines that do not have a device type. I have... (2 Replies)
Discussion started by: jwinsk
2 Replies

6. UNIX for Dummies Questions & Answers

print the line immediately after a regexp; but regexp is a sentence

Good Day, Im new to scripting especially awk and sed. I just would like to ask help from you guys about a sed command that prints the line immediately after a regexp, but not the line containing the regexp. sed -n '/regexp/{n;p;}' filename What if my regexp is 3 word or a sentence. Im... (3 Replies)
Discussion started by: ownins
3 Replies

7. Shell Programming and Scripting

Passing a regexp to grep via a shell script

Hello, I have the output of ls -l stored in a text file called "files.txt". -rwx------ 1 user1 dev 130 Sep 21 16:14 sc1.sh -rwxr----- 1 user1 dev 10328 Sep 29 20:11 sc10.sh -rwxr----- 1 user1 dev 9984 Sep 30 15:33 sc11.sh -rwxr----- 1 user1 dev ... (2 Replies)
Discussion started by: rogersed
2 Replies

8. Shell Programming and Scripting

Help with regexp

Hi there! I would like to know how to find and replace all numbers in a *.html file and make them bold. Any help will be appreciated! :) (7 Replies)
Discussion started by: agasamapetilon
7 Replies

9. UNIX for Dummies Questions & Answers

grep using regexp

I have 2 files called stuff-egress-filter and stuff-ingress filter. There are also files called something like stuff-egress-F/0 I want to match the first two... I tried (i realize there is no filename... I'm piping this from the ls command) grep stuff-*-filter Finds nothing. If I... (18 Replies)
Discussion started by: earnstaf
18 Replies

10. UNIX for Advanced & Expert Users

regexp

Hi guys, does anyone know how to test for a regular expression - i want to include it in a script to make sure the variable is a regexp cheers (1 Reply)
Discussion started by: penfold
1 Replies

Featured Tech Videos