| help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers | help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax
# 1  
Old 10-13-2009
| help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax

Hello,

I'm working on unix with grep (GNU grep) 2.5.1. I'm going through some of the newer regex syntax using Regular Expression Reference - Advanced Syntax a guide.

Code:
ls -aLl /bin | grep "\(x\)"

Which works, just highlights 'x' where ever, when ever.

I'm trying to to get (?:) to work but I just get no return.

Code:
 ls -aLl /bin | grep "\(\?\:x\)"

Code:
 ls -aLl /bin | grep "\(\?:x\)"

Code:
 ls -aLl /bin | grep "\(?\:x\)"

All of these return nothing. I'm not sure what I need to do to implement this. Like GNU grep supports the advance features like look ahead/behind, conditionals, backreference, etc?

The below code works which using grouping:
Code:
ls -aLl /bin | grep "^\([^x]*x\)\{1\}[^x]*$"

# 2  
Old 10-15-2009
I don't undestand what you problem is since it seems you found a way to solve it?
# 3  
Old 10-15-2009
Hello, I apologize if the message was unclear.

The problem is: I want to use the following advanced regular expression syntax "(?:)" which represents grouping without backreference.
Related information: From what I read GNU Grep supports the advanced syntax.
Support: I've used "()" which is grouping with backreference.

My goal isn't to solve any particular problem really. I'm new to the unix enviroment and right now I'm trying to learn as much about grep and regular expressions as possible. I understand that the advanced syntax with grep is likely going to be academic only since I would find piping into other programs simpler (sed/awk).

Summary: Can I use the syntax "(?:)" with GNU Grep 2.5.1?
# 4  
Old 10-15-2009
According to "GNU Extended Regular Expressions", the EREs are just like the usual, basic grep REs, only that special characters for grouping, repetition, ... don't need to be escaped. I've seen non-capturing groups, zero-width assertions, ... in Perl and the PCRE library so far (don't know about Python, Ruby, ... feel free to correct me).
# 5  
Old 10-15-2009
Ok, cool. You gave me some insight. For reference, the below line returns lines with exactly 3 matches if 'x' in the entire line.
3 can be replaced with any positive integer.
'x' can be replaced by any string.

Code:
grep "^\([^x]*x\)\{3\}[^x]*$" /usr/share/dict/words
grep -E "^([^x]*x){3}[^x]*$" /usr/share/dict/words

Addinging the option -E allowed to removed the "\" for the special characters and the code worked as expected.
There is also -P option for Perl extensions.

Code:
grep -E "^(?:[^x]*x){3}[^x]*$" /usr/share/dict/words
grep -P "^(?:[^x]*x){3}[^x]*$" /usr/share/dict/words

-E returned nothing when I added the ?: to ()
-P returned everything

I'm getting closer, I'll just need sometime to fool around with it. Thanks for the help.

Regular Expression Reference - Advanced Syntax
UNIX man pages : grep ()
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help on grep syntax in UNIX

Dear Team /app/Appln/logs/ echo Session used server are 'grep -i pid|grep -i session | cut -d'.' -f1 | awk '{print $9}' | sort | uniq' Output - lxserver01 lxserver02 lxserver03 When I grep session pid in logs server details I can see above distinct server details but I... (6 Replies)
Discussion started by: skp
6 Replies

2. UNIX for Beginners Questions & Answers

Grep in regex

Hello guys, Here i am writing a script in bash to check for a valid URL from a file using regex This is my input file http://www.yahoo.commmmmm http://www.google.com https://www.gooogle.co www.test6.co.in www.gmail.com www.google.co htt://www.money.com http://eeeess.google.com... (2 Replies)
Discussion started by: Meeran Rizvi
2 Replies

3. Shell Programming and Scripting

grep -v and regex

How to match lines that don't contain a patern in regex it self, without using the -v option of grep? (15 Replies)
Discussion started by: vistastar
15 Replies

4. UNIX for Dummies Questions & Answers

Advanced grep'in... grep for data next to static element.

I have a directory I need to grep which consists of numbered sub directories. The sub directory names change daily. A file resides in this main directory that shows which sub directories are FULL backups or INCREMENTAL backups. My goal is to grep the directory for the word "full" and then... (2 Replies)
Discussion started by: SysAdm2
2 Replies

5. UNIX for Dummies Questions & Answers

| help | unix | grep - Can I use grep to return a string with exactly n matches?

Hello, I looking to use grep to return a string with exactly n matches. I'm building off this: ls -aLl /bin | grep '^.\{9\}x' | tr -s ' ' -rwxr-xr-x 1 root root 632816 Nov 25 2008 vi -rwxr-xr-x 1 root root 632816 Nov 25 2008 view -rwxr-xr-x 1 root root 16008 May 25 2008... (7 Replies)
Discussion started by: MykC
7 Replies

6. UNIX for Dummies Questions & Answers

Help | Unix | grep | regular expression | backreference | Syntax/Logic

Hello, I'm working on learning regular expressions and what I can do with them. I'm using unix to and its programs to experiment and learn what my limitations are with them. I'm working on duplicating the regular expression: ^(.*)(\r?\n\1)+$ This is supposed to delete duplicate lines... (2 Replies)
Discussion started by: MykC
2 Replies

7. UNIX for Dummies Questions & Answers

Syntax Help | unix | grep | regular expression | repetition

Hello, This is my first post so, Hello World! Anyways, I'm learning how to use unix and its quickly become apparent that a strong foundation in regular expressions will make things easier. I'm not sure if my syntax is messing things up or my logic is messing things up. ps -e | grep... (4 Replies)
Discussion started by: MykC
4 Replies

8. Shell Programming and Scripting

regex and grep

I want it to find lines that contain any number of capital letters before P this is what I have tried echo "AAAAAP" | grep 'P' echo "AAAAAP" | grep '\{1\}P' echo "AAAAAP" | grep '^*P' But none of them seem to work, any help is much appreciated thanks Calypso (4 Replies)
Discussion started by: Calypso
4 Replies

9. Shell Programming and Scripting

Advanced grep and sed

I am wondering if there is a way via grep and sed to extract a string that is on the 2nd line below a known marker as in this example: TextRel 203 0 0 "WELL:" SetPosAbs 1287 -6676 TextRel 210 0 0 "AEP #2" The marker is WELL:, but the string I need is "AEP #2". Can grep/sed handle this... (19 Replies)
Discussion started by: phudgens
19 Replies

10. UNIX for Dummies Questions & Answers

correct syntax for using "grep regex filename" ?

I'm trying to grep a long ls by looking at the beginning of each filename for example: Many files begin with yong_ho_free_2005... Many files begin with yong_ho_2005... I can't just use "grep yong_ho" otherwise It'll display both files. So I'm trying to use a regex but my syntax is wrong. ... (2 Replies)
Discussion started by: yongho
2 Replies
Login or Register to Ask a Question