The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Development Releases: Linux Mint 4.0 Beta "Fluxbox", 4.0 Alpha "Debian" iBot UNIX and Linux RSS News 0 01-04-2008 03:00 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM
how could i make a program mixed with many "|", "<" and ">" strugglingman High Level Programming 2 04-29-2006 09:11 AM
No utpmx entry: you must exec "login" from lowest level "shell" peterpan UNIX for Dummies Questions & Answers 0 01-18-2006 04:15 AM
what difference between "terminal" and "console" chenhao_no1 High Level Programming 1 09-09-2002 10:46 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-10-2008
Gaurang033 Gaurang033 is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 13
Question Difference between "?" and "*"

Hi frnds,

Can anybody please tell me what is the difference between "?" and "*" Regular expression.

I have file called test.txt as follows
a
aa
aba
pqr
xyz

Both
Code:
egrep "a?" test.txt
and
Code:
egrep "a*" test.txt
both prints the same output. the Whole file.
  #2 (permalink)  
Old 07-10-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,927
egrep uses EREs (Extended Regular Expressions) From POSIX.2
Quote:
An atom followed by `*' matches a sequence of 0 or more matches of the atom. An atom followed by `+' matches a sequence of 1 or more matches of the atom. An atom followed by `?' matches a sequence of 0 or 1 matches of the atom.
Thus "a?" implies 0 or 1 matches of "a" and "a*" implies 0 or more matches of "a".

If you want to just list the lines containing a, the following will work
Code:
$ egrep "a+" file
a
aa
aba
$

Last edited by fpmurphy; 07-10-2008 at 12:27 PM..
  #3 (permalink)  
Old 07-10-2008
Gaurang033 Gaurang033 is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 13
Quote:
Originally Posted by fpmurphy View Post
Thus "a?" implies 0 or 1 matches of "a" and "a*" implies 0 or more matches of "a".
That's what i am asking. I know "a?" means 0 or 1 occurrence of preceding pattern. I was thinking it as exactly 0 or 1 occurrence. but it's not like that. I am not getting the difference between "*" and "?" . If there is any can you give me some example of it.
  #4 (permalink)  
Old 07-10-2008
krishmaths krishmaths is offline
Registered User
  
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 191
@gaurang:

Remember that you are doing a grep. That means the command will list down lines from the file that match the input pattern.

In this case egrep works on your patterns a? and a* in the following way.

egrep a? --> List down lines from test.txt if it contains an a followed by any character. Which means a, aa, abcdef, a897 all will match, and hence you get all these in the output.

egrep a* --> List down lines from test.txt if it contains an a followed by 0 or more nr of characters. Again a, aa, abcdef, a897 all will match.

Hope you get the point.

Moreover, to see the difference between ? and *, create files with names : a, aa, a897, abcd
and then do
Code:
ls a?
and
Code:
ls a*
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0