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 > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Simple newbie grep question doubleminus UNIX for Dummies Questions & Answers 5 04-06-2008 06:05 PM
Simple grep question, but I'm out of practice citygov Shell Programming and Scripting 0 08-02-2005 10:31 AM
Linux Benchmarks Makes No Sense philip_38 Linux Benchmarks 0 07-22-2005 10:29 AM
simple grep question google UNIX for Dummies Questions & Answers 5 01-27-2003 11:00 PM
Simple grep questions nitin UNIX for Dummies Questions & Answers 2 10-15-2001 12:52 AM

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

Join Date: Jul 2005
Posts: 45
Simple grep - Not sure it makes sense!

I have 3 files in directory mydir named as follows, I run the sequence of commands shown below and I have questions at the result.
File names are:
ABC_GP0
ABC_GP0.ctl
ABC_GPX

Commands and results:
$ ls /mydir/ | grep *
<-- (q1) I get nothing - OK
$ ls /mydir/ | grep a*c
ABC_GP0.ctl <-- (q2) why? the case is different isn't it? where are the rest?

$ ls ls /mydir/ | grep A*C
ls not found <-- (3) why?
ABC_GP0
ABC_GP0.ctl
ABC_GPX

Thanks for your help.
  #2 (permalink)  
Old 10-19-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
The shell's filename matching characters are not regular expressions. And if you don't quote stuff like * the shell will expand it into a list filenames before grep ever runs.

grep "A*C" will match:
C
AC
AAC
AAAC
AAAAC
and so on.
  #3 (permalink)  
Old 10-20-2005
GNMIKE GNMIKE is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 45
Very well - Thanks.
  #4 (permalink)  
Old 10-21-2005
indo1144's Avatar
indo1144 indo1144 is offline
Registered User
  
 

Join Date: Jun 2002
Location: Netherlands
Posts: 54
Quote:
Originally Posted by GNMIKE
$ ls ls /mydir/ | grep A*C
ls not found <-- (3) why?
ABC_GP0
ABC_GP0.ctl
ABC_GPX
Because you typed ls twice....
  #5 (permalink)  
Old 10-21-2005
Unbeliever Unbeliever is offline
Registered User
  
 

Join Date: Jul 2005
Location: England
Posts: 183
Quote:
Originally Posted by GNMIKE
Commands and results:
$ ls /mydir/ | grep *
<-- (q1) I get nothing - OK
$ ls /mydir/ | grep a*c
ABC_GP0.ctl <-- (q2) why? the case is different isn't it? where are the rest?
The last example has already been explained so I'll do these two.

Unfortunately the '*' charcter is used at many levels and the first is when your shell interprets it before building the actual command line to execute. it will always do this unless you escape the * using a back slash character. (As pointed out by Perderabo)

So in your first case. What happens its your shell expands * to be all the files in your current working directory (separated by spaces). So what you are actually running is something like

ls /mydir/ | grep 'file1 file2 file3 file4'

which of course doesnt work.

In the second example the shell tries to expand the 'a*c' to match any file (again in the current directory) that starts with an 'a' and ends in a 'c'. If this succeeds then you may end up running a command like:

ls /mydir/ | grep 'access.c'

Depending on how many files match the pattern 'starts with a and ends with c' in your current directory.

If *NO* files in your current directory start with 'a' and end with 'c' then the string 'a*c' gets passed to grep. Now to grep the '*' means something slightly different. It means 'zero or more of the previously matched class'. In this case you end up greping for zero or more a's followed by one c. Hence the output you see. (Its the 'c' in ctl thats being matched, not the C in ABC).

The explanation for the 3rd example is the same but for 'A' and 'C' if you take out the extra 'ls' from the command line :-)
  #6 (permalink)  
Old 10-22-2005
GNMIKE GNMIKE is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 45
Thanks for the explanation.
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 09:16 PM.


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