Issue
I noticed that sed is not giving me the same output
NOD=$(sed -n '/uid=/{s/.*uid=//;p;}' /home/user/config.xml | tail -c +2 | rev | cut -c6- | rev)
<gateway ws="ws://1.1.1.23:8333" uid="123B0X3C"/>
and result is 123B0X3C, but when i try on another endpoint
<gateway ws="ws://1.1.1.23:8333" uid="1541CCF5"/>
result is 1541CCF
Why?
Idea is to get that number inside uid option without "" and end tag characters
Solution
You can use a single sed
for this:
NOD=$(sed -n 's/.*uid="\([^"]*\).*/\1/p' /home/user/config.xml)
Here,
-n
- suppresses the default line output.*uid="\([^"]*\).*
- finds any text,uid="
text, then captures any zero or more chars other than a"
char into Group 1, and then matches the rest of the string\1
- replaces the matched line with Group 1 valuep
-p
rints the result of the successful substitution.
See an online demo:
#!/bin/bash
s='<gateway ws="ws://1.1.1.23:8333" uid="123B0X3C"/>'
NOD=$(sed -n 's/.*uid="\([^"]*\).*/\1/p' <<< "$s")
echo "$NOD"
## => 123B0X3C
Answered By - Wiktor Stribiżew Answer Checked By - Robin (WPSolving Admin)