Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • Appreciate your answer, did I miss something in my question. Do you mean that I simply can't do what I intended to do with a single grep? Commented Nov 28, 2016 at 9:51
  • 1
    @Inian, You can't easily with a single invocation of the current version of GNU grep (the one I suppose you're trying to use as it seems it supports -P and -o though that could also be the one of FreeBSD/OS/X that are rewrites of GNU grep). You can with other grep implementations like pcregrep. But I argue you're picking the wrong tool for the task. Use sed to edit streams. Commented Nov 28, 2016 at 9:54
  • I am quite easily able to do this only using bash native regex as [[ "$string" =~ .zoo.([[:digit:]]+).*:\ (.*)$ ]] and print as printf "%s\t%s\n" "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]//[[:blank:]]}" Commented Nov 28, 2016 at 9:59
  • 2
    See edit. Replaced one space with \s+ as I suppose you had more than one space after the :. Also added a way to make sure the :\s+.* only matches if .zoo.<digits> has been found beforehand. Commented Nov 28, 2016 at 10:42
  • 1
    Now this a great answer! Commented Nov 28, 2016 at 10:43