-1

I have file with text as below, I would like get the the version argument, 123abc in below case, the argument can be alphanumric.

sometext -version 123abc -mode xx

Not working:

grep -oP "\S+\s+-version\s+\K(.*)\s+" file
walker
  • 157
  • 5
  • 2
    Why not `grep -oP 'version\s+\K\S+' file`? Does your `grep` support PCRE? what do you mean by "not working"? No result, or you get `123abc -mode`? – Wiktor Stribiżew Mar 28 '23 at 13:19
  • 2
    Does this answer your question? [Using grep to get the next WORD after a match in each line](https://stackoverflow.com/questions/10971765/using-grep-to-get-the-next-word-after-a-match-in-each-line) – Wiktor Stribiżew Mar 28 '23 at 13:24
  • 1
    What would you like to do with `sometext -version "123 abc" ...` or `sometext -version 123\ abc` or `sometext -version "abc123"` or `sometext -version 'abc123'` or `sometext -version \n\t\r123` or ... Parsing text like this is extremely fragile, and my guess is that you have a problem at a different level. Rather than scraping the information out of this file, create a single source of truth for the version and use that to generate this file. – William Pursell Mar 28 '23 at 13:28

1 Answers1

0

Using any sed:

$ sed 's/.*-version  *\([^ ]*\).*/\1/' file
123abc

Trying to use grep -P to select parts of input lines other than the matching string is just overly complicated IMHO as well as being non-portable.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185