-1

I have to get the value between the two "key" words using either awk or sed or if it is some other tool also I am fine. I have tried below code, I am not getting the desired output.

echo "<response status = 'success'><result><key>LUFRPhdfhhfufhroghyuefbFJyaEkwamhjMnRxVW9ZWT1854165442156744ZiZlljbjlHSlBjNXgwMzVJcz0=</key></result></response>" |  awk -v FS="key" 'NF>1{print $2}' 

Output I expect is:

LUFRPhdfhhfufhroghyuefbFJyaEkwamhjMnRxVW9ZWT1854165442156744ZiZlljbjlHSlBjNXgwMzVJcz0=
jas
  • 10,715
  • 2
  • 30
  • 41
saffron
  • 57
  • 1
  • 10
  • 1
    Looks like XML, and if so you'd better use an XML parser. Anyway with GNU sed : `sed -En 's@.*(.*?).*@\1@p'` – Aaron Sep 20 '19 at 12:58
  • Look at this post : https://stackoverflow.com/questions/14891743/extract-a-substring-between-two-characters-in-a-string-php/14891816 – DEVLOGIN Sep 20 '19 at 13:01

2 Answers2

3

Probably best to use an xpath expression here:

echo '<response status="success"><result><key>LUFRPhdfhhfufhroghyuefbFJyaEkwamhjMnRxVW9ZWT1854165442156744ZiZlljbjlHSlBjNXgwMzVJcz0=</key></result></response>' | \
xmllint --xpath '/response/result/key/text()' -

LUFRPhdfhhfufhroghyuefbFJyaEkwamhjMnRxVW9ZWT1854165442156744ZiZlljbjlHSlBjNXgwMzVJcz0=
Cole Tierney
  • 9,571
  • 1
  • 27
  • 35
0

Just fix your field separator FS:

echo ... |  awk -v FS="(<key>|</key>)" 'NF>1{print $2}' 
LUFRPhdfhhfufhroghyuefbFJyaEkwamhjMnRxVW9ZWT1854165442156744ZiZlljbjlHSlBjNXgwMzVJcz0=
jas
  • 10,715
  • 2
  • 30
  • 41