get the value from a response enclosed tag <>

Hi All,

I'm a newbie here, I'm having problem getting the value inside the tag of input file.

ex.
script
[unix] $ curl -H ...
response from script
<tag1>response1</tag1><tag2>response2</tag2><tag3>response3</tag3>

How to get the response from enclosed tag? is this possible?
sample output:
response1
response2
response3

Thanks,

Code is specificallyt for the input you have specified...

awk -F"<|>" '{for(i=3;i<=NF;i=i+4){print $i}}' inputfile

--ahamed

1 Like

thank you very much Sir ahamed. but what i'm trying to say is only the "response2" under tag2. is my output.

example output:
response2

Thanks,

$ nawk -F"[><]" '{print $7}' infile
response2 
1 Like

thank you very much Sir jayan jay.

I'm just wondering what is the meaning of your code. see below

$ nawk -F"[><]" '{print $3,$7,$11}' infile

---------- Post updated at 03:24 PM ---------- Previous update was at 03:06 PM ----------

thank you very much to all... but what i want to say is for example the input file is not consistent and a lot of enclosed tag are in the input file. what i want is only the value inside the <reason> tag. because the file is not consistent

ex.
<tag1>niks1</tag1><tag2>niks2</tag2><reason>YES</reason><pogi>oh yeah</pogi>

sometimes the input are in this format:
<tag1>niks1</tag1><reason>NO</reason><tag2>niks2</tag2>

that's why due to the inconsistent of file i want only to capture the value of <reason> tag.

Please advise,
Thanks

Try this:

sed 's#.*reason>\(.*\)</reason.*#\1#' file
1 Like

Thank you very much, i already got the logic. but the part that i need to the value from the enclosed tag from the response of the script without the response getting redirected to a file

script
[unix] $ curl -H ...
response from script
<tag1>response1</tag1><tag2>response2</tag2><tag3>response3</tag3>

without redirecting the response of the script to a file
sample output:
response2

Thanks,

# sedx='sed "s/.*$x>\(.*\)<\/*$x.*/\1/"' 

# x=tag2
# echo '<tag1>response1</tag1><tag2>response2</tag2><tag3>response3</tag3>'|eval $sedx
response2

# x=reason
# echo '<tag1>niks1</tag1><tag2>niks2</tag2><reason>YES</reason><pogi>oh yeah</pogi>'|eval $sedx
YES

# echo '<tag1>niks1</tag1><reason>NO</reason><tag2>niks2</tag2>'|eval $sedx
NO

hank you very much, i already got the logic. but the part that i need to the value from the enclosed tag from the response of the script without the response getting redirected to a file

script
[unix] $ curl -H ...
response from script
<tag1>response1</tag1><tag2>response2</tag2><tag3>response3</tag3>

without redirecting the response of the script to a file
sample output:
response2

Thanks,

curl -H ....|sed 's/[></]*\|tag[^2]//g;s/.*tag2\(.*\)tag2.*/\1/'
response2

regards
ygemici

curl -H ... | sed 's#.*reason>\(.*\)</reason.*#\1#'