Search code examples
linuxshellawksedposix

Extract multiple values using regex and reformat them into a new string in a shell oneliner?


Lets say for a simple example that I have a bunch of lines like this:

(1,2)
(3,4)

And I want to transform them into something like this:

second value: 2; first value: 1
second value: 4; first value: 3

In general I want to extract those values using a regex and then use them as variables for formatting an output string.

Can this be done in a shell oneliner using sed/awk or something similar?


Solution

  • You can do it with one s command:

    sed 's/(\([0-9]*\),\([0-9]*\))/second value: \2; first value: \1/' file