2

I have strings that end with:

...", "VERI-1000")

and I am trying to use sed (or any other tool) to have them formatted like

...", "(VERI-1000)" uid="1000"/>

The "VERI" and the "1000" values change multiple times so it's not always "VERI" and not always "1000"

I suppose then I am trying to search for the numerical value that precedes the ") string and then insert that value after uid="

2 Answers 2

2

Try this sed:

sed -i.bak 's~"\([^"-]*\)-\([0-9]*\)")~"(\1-\2") uid="\2"/>~' file
Sign up to request clarification or add additional context in comments.

Comments

0

With GNU sed you can do something like this:

sed -r 's_([[:digit:]]+)"\)$_& uid="\1"/>_'

The command replaces this:

([[:digit:]]+)"\)$
^ start numbered group (number 1)
 ^^^^^^^^^^^^ any number of digits
             ^ close numbered group
              ^^^^ quote, bracket, EOL

with this:

& uid="\1"/>
^ all of the matched string
       ^^ contents of group number 1

Example:

sed -r 's_([[:digit:]]+)"\)$_& uid="\1"/>_' <<<'...", "VERI-1000")'
...", "VERI-1000") uid="1000"/>

POSIX-compatible way:

sed 's_\([[:digit:]]\{1,\}\)")$_& uid="\1"/>_'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.