grep command help

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 16
Reputation: AcidG3rm5 has a little shameless behaviour in the past 
Solved Threads: 0
AcidG3rm5 AcidG3rm5 is offline Offline
Newbie Poster

grep command help

 
0
  #1
Nov 24th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: grep command help

 
1
  #2
Nov 24th, 2008
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'
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum


Views: 907 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC