Grouping in grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grouping in grep
# 1  
Old 06-18-2012
Grouping in grep

How do you do grouping in grep? Here's how I tried it at first:
Code:
egrep 'qualit(y|ies)' /usr/share/dict/words
-bash: syntax error near unexpected token `('

I'm using GNUgrep, and I found this on their site. grep regular expression syntax

Quote:
Grouping is performed with backslashes followed by parentheses ‘\(', ‘\)'. A backslash followed by a digit acts as a back-reference and matches the same thing as the previous grouped expression indicated by that number. For example ‘\2' matches the second group expression. The order of group expressions is determined by the position of their opening parenthesis ‘\('.

The alternation operator is ‘\|'.
So I tried this:
Code:
egrep 'qualit\(y\|ies\)' /usr/share/dict/words

And got no results, although clearly these two words are in the dictionary, (yeah, I even checked). Now, I could've sworn I used parentheses in grep for grouping, and the pipe as an "or" operator. So I thought maybe it was GNUgrep that was the problem, so to speak, so I tried the old /bin/grep, but got similar results. Clearly I'm going something wrong. What obvious thing am I missing?
# 2  
Old 06-18-2012
It looks like you're not using the quoting character. It should be ', not .

Note that the error is not from egrep; it's from bash. In the second example, since those backslashes do not occur within a quoted string, they are being interpreted by the shell, to whom they quote the following character. The special shell character generating the syntax error in the first example, (, in the second is quoted with a backslash, hence no syntax error.

Also, quoting aside, the second egrep's regular expression is incorrectly specified. Those backslash escaped characters are special by default in egrep's extended regular expression flavor; you only need to quote them if you want to match them literally. In grep's basic regular expression flavor, the situation is reversed: those characters are ordinary by default and need to be backslashed to become special.

Regards,
Alister

Last edited by alister; 06-18-2012 at 08:12 PM..
This User Gave Thanks to alister For This Post:
# 3  
Old 06-18-2012
Quote:
Originally Posted by alister
It looks like you're not using the quoting character. It should be ', not .

Note that the error is not from egrep; it's from bash. In the second example, since those backslashes do not occur within a quoted string, they are being interpreted by the shell, to whom they quote the following character. The special shell character generating the syntax error in the first example, (, in the second is quoted with a backslash, hence no syntax error.
That's weird. I'm not even sure how I got that character. Guess I had my finger in the wrong place, once again. ; )
Great job spotting that!

Quote:
Also, quoting aside, the second egrep's regular expression is incorrectly specified. Those backslash escaped characters are special by default in egrep's extended regular expression flavor; you only need to quote them if you want to match them literally. In grep's basic regular expression flavor, the situation is reversed: those characters are ordinary by default and need to be backslashed to become special.

Regards,
Alister
Well that's what I thought; escaping makes them literal. But then I read that bit on the GNU grep page. Maybe that applies to grep rather than egrep?

Last edited by sudon't; 06-18-2012 at 08:44 PM.. Reason: minor edit
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with grouping and zipping

Hi can you please help with the below ? source file: Column1,Column2,Column3,Column4 abc,123,dir1/FXX/F19,1 abc,123,dir1/FXX/F20,1 abc,123,dir1/FXX/F23,2 abc,123,dir1/FXX/C25,2 abc,123,dir1/FXX/X25,2 abc,123,dir1/FXX/A23,3 abc,123,dir1/FXX/Z25,3 abc,123,dir1/FXX/Y25,4 I want to... (3 Replies)
Discussion started by: paul1234
3 Replies

2. Shell Programming and Scripting

Grouping and counting

Hello, I would like to group/sort a file of records by a particular field and then count how many records belong in that grouping. For example say I have the following data: 1234|"ZZZ"|"Date"|"1"|"Y"|"ABC"|""|AA ABCD|"ZZZ"|"Date"|"1"|"Y"|"ABC"|""|AA EFGH|"ZZZ"|"Date"|"1"|"Y"|"ABC"|""|BB... (14 Replies)
Discussion started by: Nik44
14 Replies

3. Shell Programming and Scripting

Name grouping

awk 'FNR==NR {a; next} $NF in a' genes.txt refseq_exons.txt > output.txt I can not figure out how to group the same name in $4 together. Basically, all the SKI together in separate rows and all the TGFB2. Thank you :). chr1 2160133 2161174 SKI chr1 218518675 218520389 TGFB2... (1 Reply)
Discussion started by: cmccabe
1 Replies

4. Shell Programming and Scripting

UNIX grouping

Hi guys, I am a complete newbie to unix and have been tasked with creating a script to group the following data (file) by hourly slots so that I can count the transactions completed within the peak hour. I am not sure how to group data like this in unix. Can anyone please help? Here is an... (1 Reply)
Discussion started by: MrMidas
1 Replies

5. Shell Programming and Scripting

Grouping

Hi all, I am using following command: perl program.pl input.txt output.txt CUTOFF 3 > groups_3.txt containing program.pl, two files (input.txt, output.txt) and getting output in groups_3.txt: But, I wish to have 30 files corresponding to each CUTOFF ranging from 0 to 30 using the same... (1 Reply)
Discussion started by: bioinfo
1 Replies

6. Shell Programming and Scripting

Selective grouping

I have a text file in this format. Group: AAA Notes: IP : 11.11.11.11 #User xxxxxxxxx #Password aaaaaaaaaaaaaaaa Group: AAA Notes: IP : 11.11.11.22 #User yyyyyyyyyyyyy #Password bbbbbbbbbbbbb (8 Replies)
Discussion started by: anil510
8 Replies

7. Shell Programming and Scripting

Grouping each 2 lines

Whats a good way to group (by adding a new integer to the front of each line) pairs of lines, such that lines 1 & 2 are group 1, lines 3 & 4 are group 2, etc... ex input: A B C D etc... ex output: 1A 1B 2C 2D etc... (5 Replies)
Discussion started by: watingo
5 Replies

8. UNIX for Dummies Questions & Answers

Help with data grouping

Hi all, I have a set data as shown below, and i would like to eliminate the name that no children - boy and girl. What is the appropriate command can i use(other than grep)? Please assist... My input: name sex marital status children - boy children - girl ... (3 Replies)
Discussion started by: 793589
3 Replies

9. UNIX for Advanced & Expert Users

Regarding the user grouping

I am new to the unix server. My question is: i have 10 users a b c d e f g h i j Among the 10 5 users are (normal) user and remainging 5 users are (qad) users How can i sepearte this one? How can i give the dlc access rights? Please provide the clear cut idea. (0 Replies)
Discussion started by: kingganesh04
0 Replies

10. Shell Programming and Scripting

egrep and grouping

i am using the c shell on solaris. directories i'm working with: ls -1d DIV* DIV_dental/ DIV_ibc/ DIV_ifc/ DIV_index/ DIV_pharm/ DIV_sectionI/ DIV_sectionI-title/ DIV_sectionI-toc/ DIV_sectionII-title/ DIV_sectionII-toc/ DIV_standing/ DIV_standing-toc/ DIV_title/ DIV_vision/ (1 Reply)
Discussion started by: effigy
1 Replies
Login or Register to Ask a Question