0

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.

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Salem
1

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'

Votes + Comments
Good to have you back ;)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.