Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Simple grep - Not sure it makes sense! Post 87222 by Unbeliever on Friday 21st of October 2005 07:55:12 AM
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 :-)
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

7. 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

8. 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

9. 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
grep(1) 						      General Commands Manual							   grep(1)

Name
       grep, egrep, fgrep - search file for regular expression

Syntax
       grep [option...] expression [file...]

       egrep [option...] [expression] [file...]

       fgrep [option...] [strings] [file]

Description
       Commands  of  the family search the input files (standard input default) for lines matching a pattern.  Normally, each line found is copied
       to the standard output.

       The command patterns are limited regular expressions in the style of which uses a compact nondeterministic algorithm.  The command patterns
       are  full  regular  expressions.  The command uses a fast deterministic algorithm that sometimes needs exponential space.  The command pat-
       terns are fixed strings.  The command is fast and compact.

       In all cases the file name is shown if there is more than one input file.  Take care when using the characters $ * [ ^ | ( ) and   in  the
       expression because they are also meaningful to the Shell.  It is safest to enclose the entire expression argument in single quotes ' '.

       The command searches for lines that contain one of the (new line-separated) strings.

       The command accepts extended regular expressions.  In the following description `character' excludes new line:

	      A  followed by a single character other than new line matches that character.

	      The character ^ matches the beginning of a line.

	      The character $ matches the end of a line.

	      A .  (dot) matches any character.

	      A single character not otherwise endowed with special meaning matches that character.

	      A  string  enclosed in brackets [] matches any single character from the string.	Ranges of ASCII character codes may be abbreviated
	      as in `a-z0-9'.  A ] may occur only as the first character of the string.  A literal - must be placed where it can't be mistaken	as
	      a range indicator.

	      A  regular  expression  followed	by  an	* (asterisk) matches a sequence of 0 or more matches of the regular expression.  A regular
	      expression followed by a + (plus) matches a sequence of 1 or more matches of the regular expression.  A regular expression  followed
	      by a ? (question mark) matches a sequence of 0 or 1 matches of the regular expression.

	      Two regular expressions concatenated match a match of the first followed by a match of the second.

	      Two regular expressions separated by | or new line match either a match for the first or a match for the second.

	      A regular expression enclosed in parentheses matches a match for the regular expression.

       The  order  of  precedence  of  operators at the same parenthesis level is the following:  [], then *+?, then concatenation, then | and new
       line.

Options
       -b	   Precedes each output line with its block number.  This is sometimes useful in locating disk block numbers by context.

       -c	   Produces count of matching lines only.

       -e expression
		   Uses next argument as expression that begins with a minus (-).

       -f file	   Takes regular expression (egrep) or string list (fgrep) from file.

       -i	   Considers upper and lowercase letter identical in making comparisons and only).

       -l	   Lists files with matching lines only once, separated by a new line.

       -n	   Precedes each matching line with its line number.

       -s	   Silent mode and nothing is printed (except error messages).	This is useful for checking the error status (see DIAGNOSTICS).

       -v	   Displays all lines that do not match specified expression.

       -w	   Searches for an expression as for a word (as if surrounded by `<' and `>').  For further information, see only.

       -x	   Prints exact lines matched in their entirety only).

Restrictions
       Lines are limited to 256 characters; longer lines are truncated.

Diagnostics
       Exit status is 0 if any matches are found, 1 if none, 2 for syntax errors or inaccessible files.

See Also
       ex(1), sed(1), sh(1)

																	   grep(1)
All times are GMT -4. The time now is 09:08 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy