Timeline for How can I output only captured groups with sed?
Current License: CC BY-SA 4.0
21 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jul 15, 2023 at 19:51 | comment | added | Matthias Braun |
sed -E also works on Linux. The manual says "for portability use POSIX -E". Maybe -r can be entirely replaced in this answer with -E .
|
|
S May 16, 2023 at 15:53 | history | suggested | Daniel Griscom | CC BY-SA 4.0 |
Clarify sed command line flags
|
May 16, 2023 at 13:22 | review | Suggested edits | |||
S May 16, 2023 at 15:53 | |||||
Jun 24, 2022 at 15:55 | history | edited | Dennis Williamson | CC BY-SA 4.0 |
additional clarification
|
Jun 24, 2022 at 15:55 | comment | added | Dennis Williamson | @Myridium: I edited my answer to address that. | |
Jan 14, 2022 at 3:51 | comment | added | Myridium |
This answer relies on knowing how many numbers will occur on the line. The question specifically asks about getting this done with the capture group ([\d]+) , meaning specifying a cluster of numbers and printing however many matches there may be on that line.
|
|
Sep 20, 2021 at 12:00 | comment | added | Ziyuan |
sed outputs the input instead of the empty string when there is no match, while grep works as expected.
|
|
Feb 27, 2020 at 22:59 | comment | added | Stephen P |
This works fine for me without the -n and the /p — because you are substituting the whole string and outputting only \1 \2 — so "exclude what you don't want" really isn't the key
|
|
May 21, 2018 at 16:49 | history | edited | Dennis Williamson | CC BY-SA 4.0 |
expanded explanation
|
May 21, 2018 at 16:34 | comment | added | Dennis Williamson |
@NoahHuppert: You don't need to escape the parentheses if you use extended regex, as I have in my example, by including the -r option. I agree that I can highlight the capturing in my answer. I'll edit it accordingly. The reason the regex is large is because it implements the functionality that the OP was looking for in the Perl-style expression \d and the given input string.
|
|
May 20, 2018 at 16:58 | comment | added | Noah Huppert |
I found the accepted answer confusing b/c it incorporated a large regexp with the example, making it hard to extract the needed information: In sed you must escape parenthesis \(.*\) , access capture groups with \1 , \2 , ect..
|
|
May 9, 2014 at 10:51 | comment | added | Dennis Williamson |
@lumbric: If you're referring to the sed example, if you use the -r option (or -E for OS X, IIRC) you don't need to escape the parentheses. The difference is that between basic regular expressions and extended regular expressions (-r ).
|
|
May 9, 2014 at 9:40 | comment | added | lumbric | Note that you might need to prefix the '(' and ')' with '\', I don't know why. | |
Sep 2, 2013 at 23:44 | comment | added | drevicko |
On OSX (including Mountain Lion) you can use brew to install grep from homebrew-dupes and then use the (rather useful) -P option (:
|
|
Dec 11, 2012 at 13:08 | comment | added | avgvstvs | Ask your sysadmin to install gsed. You'd be amazed at what a few donuts will get you... | |
Oct 23, 2012 at 15:42 | comment | added | Daniel Kats | As a side-note, grep -o option is not supported on Solaris 9. Also, Solaris 9 does not support the sed -r option. :( | |
Aug 9, 2012 at 15:20 | comment | added | yincrash | As a note, OSX Mountain Lion no longer supports PCRE in grep. | |
Jan 7, 2012 at 21:28 | history | edited | Dennis Williamson | CC BY-SA 3.0 |
added another version
|
May 9, 2010 at 12:59 | history | edited | Dennis Williamson | CC BY-SA 2.5 |
clarification
|
May 9, 2010 at 11:37 | vote | accept | Pablo | ||
May 6, 2010 at 2:39 | history | answered | Dennis Williamson | CC BY-SA 2.5 |