943,824 Members | Top Members by Rank

Ad:
Nov 24th, 2008
0

grep command help

Expand Post »
I have the following information in the text file:
1:boot
2:book
3:booze
4:machine
5:boots
6:bungie
7:bark
8:aardvark
9:broken$tuff
10:robots
11:rebine

when i did a grep -n "b*" a_file.txt, it returned
1:boot
2:book
3:booze
4:machine
5:boots
6:bungie
7:bark
8:aardvark
9:broken$tuff
10:robots
11:rebine

I thought it would match the letter b followed by anything. But why is line 4,8,10,11 appearing since its not starting with letter B? I actually quite confused how to correctly use the * wildcard.

I have made a search online, and found that, if i wanted to search for anything that starts with the letter b, i should instead do a grep -n "^b" a_file.txt.

Anyone can explain to me what grep -n "b*" a_file.txt does? as in, "b*". I can't understand how it is matched.

Many thanks.
Similar Threads
Reputation Points: 6
Solved Threads: 0
Light Poster
AcidG3rm5 is offline Offline
25 posts
since Oct 2008
Nov 24th, 2008
1

Re: grep command help

b* means zero-or-more occurrences of b
And since even "hello" has zero or more instances of b, then it gets matches. This is somewhat different to the pattern matching for filenames at the command prompt.

If you want to match any b, then it's just 'b'
For 'b' at the start, it's '^b'
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: copying multiple files with user input
Next Thread in Shell Scripting Forum Timeline: Shell script help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC