Extract Value between XML tags with SED

后端 未结 3 1813
礼貌的吻别
礼貌的吻别 2021-01-22 04:36

I am receiving data from a server in the following format:

gin1601DepthOfBook<         


        
相关标签:
3条回答
  • 2021-01-22 05:20

    Something like this would work:

     sed -e 's/<\/sessionId>/<\/sessionId>\n/g' | sed -n 's/.*<sessionId>\([^<]*\)<\/sessionId>.*/\1/p'
    

    First part is because sed tries to eat up as much of a single line as possible when matching, this will find all sessionId occurences and split them up on a line on its own.

    Next part matches stuff between the sessionId tags.

    0 讨论(0)
  • 2021-01-22 05:31

    This might work for you (GNU sed):

    sed '/<sessionId>/!d;s//\n/;s/[^\n]*\n//;:a;$!{/<\/sessionId>/!N;//!ba};y/\n/ /;s/<\/sessionId>/\n/;P;D' file
    
    0 讨论(0)
  • 2021-01-22 05:38

    I would suggest using XPath, which is an XML query language. If you have the Perl XML::XPath module installed then you could simply use the following command in your shell:

    xpath -q -e '//sessionId/text()' <input_file>
    
    0 讨论(0)
提交回复
热议问题