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.

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'

commented: Good to have you back ;) +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.