LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-11-2012, 04:08 PM   #1
elalexluna83
Member
 
Registered: Mar 2012
Posts: 55

Rep: Reputation: Disabled
BASH - regular expression


Hi guys

i have a file with the following content

$ cat num2
100
101
1001
10001
100001
1000001

i'd like to match only 1001, 10001, 100001

i did this but it doesn't work

$ grep 10\{2,3\}1 num2
$

any idea?
 
Old 09-11-2012, 04:11 PM   #2
elalexluna83
Member
 
Registered: Mar 2012
Posts: 55

Original Poster
Rep: Reputation: Disabled
it worked with egrep

$ egrep 10\{2,4\}1 num2
1001
10001
100001
 
Old 09-11-2012, 06:08 PM   #3
rosehosting.com
Member
 
Registered: Jun 2012
Location: Missouri, USA
Posts: 236

Rep: Reputation: 64
Quote:
Originally Posted by elalexluna83 View Post
it worked with egrep

$ egrep 10\{2,4\}1 num2
1001
10001
100001
it may be more precise to use something like this:

Code:
grep -E '^10{2,4}1$' /tmp/num2
1001
10001
100001
 
Old 09-12-2012, 10:23 AM   #4
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
1) grep and sed support two levels of regular expressions "basic", and "extended". Only the extended regex offers things like "{}". To enable them use grep -E and sed -r. egrep is a synonym for grep -E and is apparently deprecated.

However, when in basic regex mode, gnu grep and sed also allow you to activate extended regex characters individually by preceding them with backslashes. Escaping them when in extended regex mode, OTOH, does the reverse, and disables them. The grep man page has a whole section on basic vs. extended regex.

So your expression is actually ok in this regard (although it really needs to be anchored on each end, as rosehosting posted).


2) The real problem you are encountering is actually the behavior of the shell. The shell reduces all backslashed characters to their literal equivalents, so "\{" is converted back to "{" before the command is executed. You're right back to using basic regex. The backslash also needs to be protected so that it can be properly passed to grep.

This is why you should always quote the expression in commands like this.
 
  


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
[SOLVED] bash script using regular expression edwardcode Programming 5 05-31-2012 02:07 AM
Bash Script / Regular Expression Problem rm_-rf_windows Linux - General 4 03-28-2012 01:05 PM
Regular expression help smeezekitty Programming 7 03-27-2011 07:57 PM
[SOLVED] [bash] rm regular expression help RaptorX Programming 26 08-01-2009 06:29 PM
bash: checking if a variable is a number (need regular expression help) anonguy9 Linux - Newbie 6 03-29-2009 02:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:39 AM.

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