Home > Software design >  Extract WORD between two constant strings
Extract WORD between two constant strings

Time:05-01

I have the following string

xml_srx_name="<name>SRX-NAME</name>"

I am trying to print the text between > and < so it would print SRX-NAME

I am really close but this is what I get: >SRX-NAME< which is what I was able to achieve with this command:

$ cat $xml_srx_name | awk '/SRX-NAME/ {print $1}' | grep -oPz "(?s)>.*?<" | tr '\0' '\n'

Output:
>SRX-NAME<

CodePudding user response:

You can try

  1. Add | tr -d '<>' in the end
  2. Use cat … |grep -o SRX-NAME
  3. Use cat … |cut -d \> -f 2 | cut -d \< -f 1

CodePudding user response:

input="test<hello>text"

rightpart=${input#*<}   # remove prefix up to "<" (included)
output=${rightpart%>*}  # remove suffix from ">" (included)

echo $output

Or

Using extglob in bash, you can do this in single step:

shopt -s extglob
input='test<hello>heythere'
echo "${input//@(*<|>*)/}"

Here @(<|>) matches a substring from start to < character OR a substring from > to end. Using // we replace all such instances with an empty string.

CodePudding user response:

If you end up with a compound command that uses cat, awk, grep and tr you probably have a nice example of anti-pattern. awk alone is enough:

$ xml_srx_name="<name>SRX-NAME</name>"
$ awk -F'<|>' '/SRX-NAME/ {print $3}' <<< "$xml_srx_name"
SRX-NAME

Or with sed:

$ sed '/SRX-NAME/s/.*>\(.*\)<.*/\1/' <<< "$xml_srx_name"
SRX-NAME
  • Related