Timeline for answer to grep -w matches only the first occurrence of a pattern in a line by pLumo
Current License: CC BY-SA 4.0
Post Revisions
15 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Mar 3, 2022 at 18:15 | comment | added | Stéphane Chazelas |
(when I said ast-open works fine above, I meant it works as per the more usual meaning of -w like in the original BSD implementation, it outputs n1, n2, n3, not n1= n2= n3=). Note that in any case, none of -w, -o, -P are standard grep options.
|
|
| Mar 3, 2022 at 18:10 | comment | added | Stéphane Chazelas |
@aviro, \> matches between a word and non-word characters (or the end of the subject). = is not a word characters, so \> can never match after it. AFAIK, -w was added first to 2BSD grep is the late 70s along with \<, \>, and was just adding \< and \> around the regexp. GNU grep's -w is documented to behave differently, I don't know why they deviated from the BSD behaviour. But it goes to say that using -w to try and match things that don't begin or end with a word character is not portable.
|
|
| Mar 3, 2022 at 17:30 | comment | added | aviro |
@StéphaneChazelas, grep -o '\<n[0-9]=*\>' doesn't do exactly what I was expecting; = signals the end of the word, so in case the string is n1=, (without any word characters following) it will only show n1 instead of n1=.
|
|
| Mar 3, 2022 at 16:29 | comment | added | Stéphane Chazelas |
With the busybox or toybox implementations, grep -ow "n[0-9]=*" finds nothing. The ast-open implementation seems fine in that regard.
|
|
| Mar 3, 2022 at 16:27 | comment | added | Stéphane Chazelas |
FWIW, grep -o '\<n[0-9]=*\>' or even grep -wo '\<n[0-9]=*\>' seems to work fine.
|
|
| Mar 3, 2022 at 15:49 | comment | added | aviro |
Thanks, the Perl problem was probably fixed in a later version of GNU grep, I have version 3.1.
|
|
| Mar 3, 2022 at 15:06 | history | edited | pLumo | CC BY-SA 4.0 |
added 253 characters in body
|
| Mar 3, 2022 at 14:55 | history | edited | pLumo | CC BY-SA 4.0 |
deleted 113 characters in body
|
| Mar 3, 2022 at 14:51 | comment | added | pLumo | You're right, removed that bit ... Added a workaround though ;-) | |
| Mar 3, 2022 at 14:49 | history | edited | pLumo | CC BY-SA 4.0 |
deleted 331 characters in body
|
| Mar 3, 2022 at 14:42 | history | edited | pLumo | CC BY-SA 4.0 |
added 28 characters in body
|
| Mar 3, 2022 at 14:28 | history | edited | pLumo | CC BY-SA 4.0 |
added 134 characters in body
|
| Mar 3, 2022 at 14:23 | comment | added | aviro |
Regarding the statement that "the pattern doesn't make too much sense", see my comment to my question. Also, your sed example is not relevant, since it's not equivalent to the -w flag in grep.
|
|
| Mar 3, 2022 at 14:22 | history | edited | pLumo | CC BY-SA 4.0 |
added 1 character in body
|
| Mar 3, 2022 at 14:17 | history | answered | pLumo | CC BY-SA 4.0 |