![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 | 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
Quote:
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 :-) |
![]() |
| Bookmarks |
| Tags |
| regex, regular expressions |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|