-2

This question already has an answer here:

I have a log file with multiple request XML snippets in it. I am using this sed command to extract and print the request XML:

sed -n '/<GetCompensableProductIdentification*/,/<\/GetCompensableProductIdentification>/p' ProductIdentifierService.log

I want to use variables for the request start and end tags so that I can filter out any request I want. How can I do that?

marked as duplicate by Rahul, thrig, Kusalananda, Jeff Schaller, sam Aug 2 '16 at 17:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

This might help you,

 sed -n 's,.*<GetCompensableProductIdentification>\(.*\)</GetCompensableProductIdentification>.*,\1,p' input

But I guess xmlstarlet is valid tool for this case,

 xmlstarlet sel -t -v '//GetCompensableProductIdentification' -n
  • 3
    I'd definitely go with the proper XML parsing tool, especially if the XML is complete. +1 – Kusalananda Aug 2 '16 at 16:33
1

This script

sed -n '/<GetCompensableProductIdentification>/,/<\/GetCompensableProductIdentification>/p' ProductIdentifierService.log

should do it

The -n option here suppresses the normal output. so, only, the stuff you selected will be printed.


To use variables, you could use the [ shell parameter substitution ].

start_tag="<GetCompensableProductIdentification>"
end_tag="${start_tag/#</<\\/}"
sed -n "/${start_tag}/,/${end_tag}/p" ProductIdentifierService.log

That tempts you to write a script

# Script name - xmlparser.sh
#!/usr/bin/env bash
start_tag="$1"
end_tag="${start_tag/#</<\\/}"
if [ -e "$2" ] # Extra check if file exists
then
   sed -n "/${start_tag}/,/${end_tag}/p" "$2"
else
   echo " The file : $2 doesn't exists"
fi

Run the script like

./xmlparser.sh "<GetCompensableProductIdentification>"  "/path/to/log/file"
  • says ,./Log_ExtractorN[37]: end_tag="${start_tag/#</<\\/}": bad substitution – Abhinav Singh Aug 2 '16 at 16:23
  • I am using korn shell (ksh) – Abhinav Singh Aug 2 '16 at 16:27
  • 1
    I am running it with a script on my exceed unix terminal , – Abhinav Singh Aug 2 '16 at 16:35
  • The scripts has the following , – Abhinav Singh Aug 2 '16 at 16:35
  • 2
    Please move this conversation to a discussion chat. Also please try and cleanup the above chats and move anything you can into the actual question and/or delete it if it's no longer necessary. – slm Aug 2 '16 at 18:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.