I have a string of form FOO_123_BAR.bazquux
, where FOO
and BAR
are fixed strings, 123
is a number and bazquux
is freeform text.
I need to perform a text transformation on this string: extract 123
and bazquux
, increment the number and then arrange them in a different string.
For example, FOO_123_BAR.bazquux
⇒ FOO=124 BAR=bazquux
.
(Actual transformation is more complex.)
Naturally, I can do this in a sequence of sed and expr calls, but it's ugly:
shopt -s lastpipe
in=FOO_123_BAR.bazquux
echo "$in" | sed -r 's|^FOO_([0-9]+)_BAR\.(.+)$|\1 \2|' | read number text
out="FOO=$((number + 1)) BAR=$text"
Is there a more powerful text processing tool that can do the job in a single invocation? If yes, then how?
Edit: I apologize for not making this clearer, but the exact structure of the input and output is an example. Thus, I prefer general solutions that work with any delimiters or absence thereof, rather than solutions that depend on e. g. presence of underscores.