Simple grep - Not sure it makes sense!


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Simple grep - Not sure it makes sense!
# 1  
Old 10-19-2005
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  
Old 10-19-2005
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  
Old 10-20-2005
Very well - Thanks.
# 4  
Old 10-21-2005
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  
Old 10-21-2005
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  
Old 10-22-2005
Thanks for the explanation.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Text Based Game, This Error Makes NO sense. Please help

Okay so I'm making a simple text based game that branches into different scenarios. By branching I mean branching off into whole different files with that part of the game in it. I got tired of working on scenario 1 so I'm working on scenario 2. As I get started and try to test it, I get an... (3 Replies)
Discussion started by: lemonoid
3 Replies

2. Programming

This Makes NO sense. I'm making a game and getting an error, need help.

Okay so I'm making a simple text based game that branches into different scenarios. By branching I mean branching off into whole different files with that part of the game in it. I got tired of working on scenario 1 so I'm working on scenario 2. As I get started and try to test it, I get an error... (1 Reply)
Discussion started by: lemonoid
1 Replies

3. Shell Programming and Scripting

Simple grep script

I'm trying to write a simple script to identify every user who tried to “sudo” on the system. I have the first portion down to grep the log file grep “sudo” /var/log/secure. What I want to do is have the script identify the person just one time not every instance the user tried... (4 Replies)
Discussion started by: bouncer
4 Replies

4. Shell Programming and Scripting

diff result makes no sense

Hi, I have written a small shellscript Imagine dbalt.txt already existed... " .... touch report.txt lynx -dump "http://site.com/index.htm" > site1.txt lynx -dump "http://site.com/index2.htm" > site2.txt grep -E 'Nummer: |EUR' site1.txt > preis1.txt grep -E 'Nummer: |EUR' site2.txt >... (2 Replies)
Discussion started by: Blackbox
2 Replies

5. Shell Programming and Scripting

BASH: How do I grep on a variable? (or simmilar question that makes sense)

Hi, I've been running code which very frequently calls books.csv. e.g: grep -i horror books.csv > tempExcept, I'm trying to move away from using temporary files or frequently calling books.csv to improve efficiency. So I tried something like bookfile=$(cat books.csv) grep -i horror... (4 Replies)
Discussion started by: Quan
4 Replies

6. Shell Programming and Scripting

simple grep is not working for me

Hi, On the log Netscape log, I need to grep for 500 error. I am doing that but I also get 1500 in that same log. cat access |grep "500" Results: "GET /css/RBR.css HTTP/1.1" 200 15000 304 - - - 399 639 523 164 0 This not what I need... Please advice. (4 Replies)
Discussion started by: samnyc
4 Replies

7. Linux Benchmarks

Linux Benchmarks Makes No Sense

I created two computers with identical hardware, and run the benchmark programs in both starting at the same exact time. What makes no sense is that the computer that has the lower average index (121) finished the race a good 30 minutes ahead of the computer wich showed the higher avg index... (0 Replies)
Discussion started by: philip_38
0 Replies

8. UNIX for Dummies Questions & Answers

simple grep question

I have seen this used several times but not really sure of what it actually does. I am confused with the second grep as the argument to the first. some commands | grep -v grep | some other commands Can anyone provide an explanation? Thanks, (5 Replies)
Discussion started by: google
5 Replies

9. UNIX for Dummies Questions & Answers

Simple grep questions

Hi all, My boss wants me to find out how often e-m users are accessing their account:confused:. The mail server keeps log of all logins. I want to use grep the 'usernames', but it should come out the moment it first encounters the username in the log. Can I do that? I want to avoid 10+ greps... (2 Replies)
Discussion started by: nitin
2 Replies
Login or Register to Ask a Question