Friday, May 27, 2022

[SOLVED] Extract WORD between two constant strings

Issue

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<

Solution

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


Answered By - Tiago Peczenyj
Answer Checked By - Terry (WPSolving Volunteer)