LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-29-2012, 09:29 PM   #1
Find
Member
 
Registered: Jan 2012
Posts: 32

Rep: Reputation: Disabled
Regular Expressions - grouping


Hello, I am a University biology student. I am trying to figure out regular expressions, but I am getting both confused and frustrated trying to group one or more test for use with grep. I was hoping someone here might be able to help.

Given the file:
Code:
a_file
with contents: "
Code:
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
"

I would like to use grep to find the value a such that the command is similar to:

Code:
grep '[\,]+' a_file
However: NONE OF THIS WORKS:
Code:
$ grep '[\,]+' a_file
$ grep '\,+' a_file
$ grep '(\,)+' a_file
$ grep '(,)+' a_file
$ grep '{\,}+' a_file
I realize I could just use the comma, but once I need a more complicated expression that would break down.


Thanks!
 
Old 01-29-2012, 09:44 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
For efficiency reasons, plain grep only handles the basic set of regular expression operators. Try using egrep or grep -E if you wish to make use of the extended set of operators (I should note that the GNU grep can use the '+' operator, but you have to escape it, eg grep ',\+', just to confuse things)

In general the term 'regular expression' is only loosely defined, and different tools do not always have exactly the same operator set.

Last edited by neonsignal; 01-29-2012 at 09:55 PM.
 
1 members found this post helpful.
Old 01-31-2012, 09:03 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
BTW, is the backslash intended to escape the comma above?

Not only does the comma not have any special regex meaning that needs escaping, but even if it did, only a few characters retain any special meaning when inside a bracket expression. And those are generally handled by careful positioning inside the brackets so that they can't be interpreted as special (the "]" must be the first character in the list, "^" can't be the first, and "-" must be first or last).

Unless your regex is actually meant to match a string of either/both commas and backslashes, in which case it's perfect.

Check out the grep man page and info page for more on how it, and the regexes it supports, operates.
 
1 members found this post helpful.
Old 01-31-2012, 10:56 AM   #4
Find
Member
 
Registered: Jan 2012
Posts: 32

Original Poster
Rep: Reputation: Disabled
Thanks 'neonsignal' and 'David the H.'! That was helpful for getting started.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expressions ziggy25 Linux - Newbie 7 11-05-2007 06:57 AM
regular expressions. stomach Linux - Software 1 02-10-2006 06:41 AM
Regular Expressions markjuggles Programming 2 05-05-2005 11:39 AM
help with REGULAR EXPRESSIONS ner Linux - General 23 10-31-2003 11:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration