What's the difference between b and < in the grep command

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite
1












In the man page of grep, I see



The symbols < and > respectively match the empty string at the beginning and 
end of a word. The symbol b matches the empty string at the edge of a word.


But I still can't figure out the difference. To me, b is Perl's notation for word boundary, while < is Vim's notation for the same purpose.

PS: English is not my native language. Pardon me if the difference is obvious to you.










share|improve this question





















  • I can't think of any useful usage of < or > where you can't substitute b. Obviously you can use < and > to create patterns that don't match anything, but that's not much use! So in practice it looks like there is no difference.
    – Graeme
    Mar 27 '14 at 9:58










  • I have to disagree I will prove it :-) I'll edit my answer to add an example
    – Kiwy
    Mar 27 '14 at 10:09










  • < predates vim (it was used in grep during the 1980s).
    – Thomas Dickey
    Apr 25 '16 at 1:01














up vote
5
down vote

favorite
1












In the man page of grep, I see



The symbols < and > respectively match the empty string at the beginning and 
end of a word. The symbol b matches the empty string at the edge of a word.


But I still can't figure out the difference. To me, b is Perl's notation for word boundary, while < is Vim's notation for the same purpose.

PS: English is not my native language. Pardon me if the difference is obvious to you.










share|improve this question





















  • I can't think of any useful usage of < or > where you can't substitute b. Obviously you can use < and > to create patterns that don't match anything, but that's not much use! So in practice it looks like there is no difference.
    – Graeme
    Mar 27 '14 at 9:58










  • I have to disagree I will prove it :-) I'll edit my answer to add an example
    – Kiwy
    Mar 27 '14 at 10:09










  • < predates vim (it was used in grep during the 1980s).
    – Thomas Dickey
    Apr 25 '16 at 1:01












up vote
5
down vote

favorite
1









up vote
5
down vote

favorite
1






1





In the man page of grep, I see



The symbols < and > respectively match the empty string at the beginning and 
end of a word. The symbol b matches the empty string at the edge of a word.


But I still can't figure out the difference. To me, b is Perl's notation for word boundary, while < is Vim's notation for the same purpose.

PS: English is not my native language. Pardon me if the difference is obvious to you.










share|improve this question













In the man page of grep, I see



The symbols < and > respectively match the empty string at the beginning and 
end of a word. The symbol b matches the empty string at the edge of a word.


But I still can't figure out the difference. To me, b is Perl's notation for word boundary, while < is Vim's notation for the same purpose.

PS: English is not my native language. Pardon me if the difference is obvious to you.







linux command-line grep






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 '14 at 9:36









duleshi

156126




156126











  • I can't think of any useful usage of < or > where you can't substitute b. Obviously you can use < and > to create patterns that don't match anything, but that's not much use! So in practice it looks like there is no difference.
    – Graeme
    Mar 27 '14 at 9:58










  • I have to disagree I will prove it :-) I'll edit my answer to add an example
    – Kiwy
    Mar 27 '14 at 10:09










  • < predates vim (it was used in grep during the 1980s).
    – Thomas Dickey
    Apr 25 '16 at 1:01
















  • I can't think of any useful usage of < or > where you can't substitute b. Obviously you can use < and > to create patterns that don't match anything, but that's not much use! So in practice it looks like there is no difference.
    – Graeme
    Mar 27 '14 at 9:58










  • I have to disagree I will prove it :-) I'll edit my answer to add an example
    – Kiwy
    Mar 27 '14 at 10:09










  • < predates vim (it was used in grep during the 1980s).
    – Thomas Dickey
    Apr 25 '16 at 1:01















I can't think of any useful usage of < or > where you can't substitute b. Obviously you can use < and > to create patterns that don't match anything, but that's not much use! So in practice it looks like there is no difference.
– Graeme
Mar 27 '14 at 9:58




I can't think of any useful usage of < or > where you can't substitute b. Obviously you can use < and > to create patterns that don't match anything, but that's not much use! So in practice it looks like there is no difference.
– Graeme
Mar 27 '14 at 9:58












I have to disagree I will prove it :-) I'll edit my answer to add an example
– Kiwy
Mar 27 '14 at 10:09




I have to disagree I will prove it :-) I'll edit my answer to add an example
– Kiwy
Mar 27 '14 at 10:09












< predates vim (it was used in grep during the 1980s).
– Thomas Dickey
Apr 25 '16 at 1:01




< predates vim (it was used in grep during the 1980s).
– Thomas Dickey
Apr 25 '16 at 1:01










1 Answer
1






active

oldest

votes

















up vote
6
down vote













< matches the beginning of a word
> matches the end of a word
b matches both boundaries if at the end or at the beginning



The important thing about those special characters is that they match an empty string and not the word boundary itself. a word boundary being the contrary of the the set of character represented by w equivalent of [_[:alnum:]] (letter a to Z, digits and _) in Posix notation.



Example



Finally, Graeme find a very interesting example:



$ echo 'acegi z' | grep -o '[acegi ]*>' | cat -A
acegi$
$ echo 'acegi z' | grep -o '[acegi ]*b' | cat -A
acegi $


Currently, this example shows that it can useful sometimes to match precisely the end of word instead of a word boundary because the use of matching space character is avoided by matching the end of word.

So in a more useful example, I would say that if you want to match non-word character and the end of this non-word, you can't use >; but maybe b can be used in this particular case because it will match the start of the next word.



So far no example manage to reach my mind.
But in my opinion, there are probably some few use cases where it make sense, but my guess is that it's onlyfor readability purpose, Because when you put b it's vague but if you precise start or end of the word then it gives a better understanding of the regexp to the persons who read it.






share|improve this answer






















  • But can you think of a situation where it is useful to use < or > instead of b?
    – Graeme
    Mar 27 '14 at 10:04






  • 1




    @Graeme: <..> would search for 2-letter words.
    – Stéphane Chazelas
    Mar 27 '14 at 10:08










  • @StephaneChazelas Ok, I see, so anything that matches a space or a word character can leave it ambiguous as to whether you are at the beginning or end of a word.
    – Graeme
    Mar 27 '14 at 10:20






  • 1




    Note those are transitions from word to non-word characters, they have nothing to do with spaces (which is just one of many non-word characters).
    – Stéphane Chazelas
    Mar 27 '14 at 10:26










  • Note that for three letter words, you'd have to write it <w3> anyway, and that would be the same as bw3b
    – Stéphane Chazelas
    Mar 27 '14 at 10:27










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f121739%2fwhats-the-difference-between-b-and-in-the-grep-command%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
6
down vote













< matches the beginning of a word
> matches the end of a word
b matches both boundaries if at the end or at the beginning



The important thing about those special characters is that they match an empty string and not the word boundary itself. a word boundary being the contrary of the the set of character represented by w equivalent of [_[:alnum:]] (letter a to Z, digits and _) in Posix notation.



Example



Finally, Graeme find a very interesting example:



$ echo 'acegi z' | grep -o '[acegi ]*>' | cat -A
acegi$
$ echo 'acegi z' | grep -o '[acegi ]*b' | cat -A
acegi $


Currently, this example shows that it can useful sometimes to match precisely the end of word instead of a word boundary because the use of matching space character is avoided by matching the end of word.

So in a more useful example, I would say that if you want to match non-word character and the end of this non-word, you can't use >; but maybe b can be used in this particular case because it will match the start of the next word.



So far no example manage to reach my mind.
But in my opinion, there are probably some few use cases where it make sense, but my guess is that it's onlyfor readability purpose, Because when you put b it's vague but if you precise start or end of the word then it gives a better understanding of the regexp to the persons who read it.






share|improve this answer






















  • But can you think of a situation where it is useful to use < or > instead of b?
    – Graeme
    Mar 27 '14 at 10:04






  • 1




    @Graeme: <..> would search for 2-letter words.
    – Stéphane Chazelas
    Mar 27 '14 at 10:08










  • @StephaneChazelas Ok, I see, so anything that matches a space or a word character can leave it ambiguous as to whether you are at the beginning or end of a word.
    – Graeme
    Mar 27 '14 at 10:20






  • 1




    Note those are transitions from word to non-word characters, they have nothing to do with spaces (which is just one of many non-word characters).
    – Stéphane Chazelas
    Mar 27 '14 at 10:26










  • Note that for three letter words, you'd have to write it <w3> anyway, and that would be the same as bw3b
    – Stéphane Chazelas
    Mar 27 '14 at 10:27














up vote
6
down vote













< matches the beginning of a word
> matches the end of a word
b matches both boundaries if at the end or at the beginning



The important thing about those special characters is that they match an empty string and not the word boundary itself. a word boundary being the contrary of the the set of character represented by w equivalent of [_[:alnum:]] (letter a to Z, digits and _) in Posix notation.



Example



Finally, Graeme find a very interesting example:



$ echo 'acegi z' | grep -o '[acegi ]*>' | cat -A
acegi$
$ echo 'acegi z' | grep -o '[acegi ]*b' | cat -A
acegi $


Currently, this example shows that it can useful sometimes to match precisely the end of word instead of a word boundary because the use of matching space character is avoided by matching the end of word.

So in a more useful example, I would say that if you want to match non-word character and the end of this non-word, you can't use >; but maybe b can be used in this particular case because it will match the start of the next word.



So far no example manage to reach my mind.
But in my opinion, there are probably some few use cases where it make sense, but my guess is that it's onlyfor readability purpose, Because when you put b it's vague but if you precise start or end of the word then it gives a better understanding of the regexp to the persons who read it.






share|improve this answer






















  • But can you think of a situation where it is useful to use < or > instead of b?
    – Graeme
    Mar 27 '14 at 10:04






  • 1




    @Graeme: <..> would search for 2-letter words.
    – Stéphane Chazelas
    Mar 27 '14 at 10:08










  • @StephaneChazelas Ok, I see, so anything that matches a space or a word character can leave it ambiguous as to whether you are at the beginning or end of a word.
    – Graeme
    Mar 27 '14 at 10:20






  • 1




    Note those are transitions from word to non-word characters, they have nothing to do with spaces (which is just one of many non-word characters).
    – Stéphane Chazelas
    Mar 27 '14 at 10:26










  • Note that for three letter words, you'd have to write it <w3> anyway, and that would be the same as bw3b
    – Stéphane Chazelas
    Mar 27 '14 at 10:27












up vote
6
down vote










up vote
6
down vote









< matches the beginning of a word
> matches the end of a word
b matches both boundaries if at the end or at the beginning



The important thing about those special characters is that they match an empty string and not the word boundary itself. a word boundary being the contrary of the the set of character represented by w equivalent of [_[:alnum:]] (letter a to Z, digits and _) in Posix notation.



Example



Finally, Graeme find a very interesting example:



$ echo 'acegi z' | grep -o '[acegi ]*>' | cat -A
acegi$
$ echo 'acegi z' | grep -o '[acegi ]*b' | cat -A
acegi $


Currently, this example shows that it can useful sometimes to match precisely the end of word instead of a word boundary because the use of matching space character is avoided by matching the end of word.

So in a more useful example, I would say that if you want to match non-word character and the end of this non-word, you can't use >; but maybe b can be used in this particular case because it will match the start of the next word.



So far no example manage to reach my mind.
But in my opinion, there are probably some few use cases where it make sense, but my guess is that it's onlyfor readability purpose, Because when you put b it's vague but if you precise start or end of the word then it gives a better understanding of the regexp to the persons who read it.






share|improve this answer














< matches the beginning of a word
> matches the end of a word
b matches both boundaries if at the end or at the beginning



The important thing about those special characters is that they match an empty string and not the word boundary itself. a word boundary being the contrary of the the set of character represented by w equivalent of [_[:alnum:]] (letter a to Z, digits and _) in Posix notation.



Example



Finally, Graeme find a very interesting example:



$ echo 'acegi z' | grep -o '[acegi ]*>' | cat -A
acegi$
$ echo 'acegi z' | grep -o '[acegi ]*b' | cat -A
acegi $


Currently, this example shows that it can useful sometimes to match precisely the end of word instead of a word boundary because the use of matching space character is avoided by matching the end of word.

So in a more useful example, I would say that if you want to match non-word character and the end of this non-word, you can't use >; but maybe b can be used in this particular case because it will match the start of the next word.



So far no example manage to reach my mind.
But in my opinion, there are probably some few use cases where it make sense, but my guess is that it's onlyfor readability purpose, Because when you put b it's vague but if you precise start or end of the word then it gives a better understanding of the regexp to the persons who read it.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jun 22 '15 at 15:17









Sildoreth

85831535




85831535










answered Mar 27 '14 at 9:43









Kiwy

5,79453455




5,79453455











  • But can you think of a situation where it is useful to use < or > instead of b?
    – Graeme
    Mar 27 '14 at 10:04






  • 1




    @Graeme: <..> would search for 2-letter words.
    – Stéphane Chazelas
    Mar 27 '14 at 10:08










  • @StephaneChazelas Ok, I see, so anything that matches a space or a word character can leave it ambiguous as to whether you are at the beginning or end of a word.
    – Graeme
    Mar 27 '14 at 10:20






  • 1




    Note those are transitions from word to non-word characters, they have nothing to do with spaces (which is just one of many non-word characters).
    – Stéphane Chazelas
    Mar 27 '14 at 10:26










  • Note that for three letter words, you'd have to write it <w3> anyway, and that would be the same as bw3b
    – Stéphane Chazelas
    Mar 27 '14 at 10:27
















  • But can you think of a situation where it is useful to use < or > instead of b?
    – Graeme
    Mar 27 '14 at 10:04






  • 1




    @Graeme: <..> would search for 2-letter words.
    – Stéphane Chazelas
    Mar 27 '14 at 10:08










  • @StephaneChazelas Ok, I see, so anything that matches a space or a word character can leave it ambiguous as to whether you are at the beginning or end of a word.
    – Graeme
    Mar 27 '14 at 10:20






  • 1




    Note those are transitions from word to non-word characters, they have nothing to do with spaces (which is just one of many non-word characters).
    – Stéphane Chazelas
    Mar 27 '14 at 10:26










  • Note that for three letter words, you'd have to write it <w3> anyway, and that would be the same as bw3b
    – Stéphane Chazelas
    Mar 27 '14 at 10:27















But can you think of a situation where it is useful to use < or > instead of b?
– Graeme
Mar 27 '14 at 10:04




But can you think of a situation where it is useful to use < or > instead of b?
– Graeme
Mar 27 '14 at 10:04




1




1




@Graeme: <..> would search for 2-letter words.
– Stéphane Chazelas
Mar 27 '14 at 10:08




@Graeme: <..> would search for 2-letter words.
– Stéphane Chazelas
Mar 27 '14 at 10:08












@StephaneChazelas Ok, I see, so anything that matches a space or a word character can leave it ambiguous as to whether you are at the beginning or end of a word.
– Graeme
Mar 27 '14 at 10:20




@StephaneChazelas Ok, I see, so anything that matches a space or a word character can leave it ambiguous as to whether you are at the beginning or end of a word.
– Graeme
Mar 27 '14 at 10:20




1




1




Note those are transitions from word to non-word characters, they have nothing to do with spaces (which is just one of many non-word characters).
– Stéphane Chazelas
Mar 27 '14 at 10:26




Note those are transitions from word to non-word characters, they have nothing to do with spaces (which is just one of many non-word characters).
– Stéphane Chazelas
Mar 27 '14 at 10:26












Note that for three letter words, you'd have to write it <w3> anyway, and that would be the same as bw3b
– Stéphane Chazelas
Mar 27 '14 at 10:27




Note that for three letter words, you'd have to write it <w3> anyway, and that would be the same as bw3b
– Stéphane Chazelas
Mar 27 '14 at 10:27

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f121739%2fwhats-the-difference-between-b-and-in-the-grep-command%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay