Need get data from XML file through shell script..

hi all, here is the sample log file and these errors are repeated in log file..
i need all the repeated time stamp ,severity and message tags needs to print in output file.. through shell script

 <log-message>
 <timestamp>2019-03-13T04:52:49.648-05:00</timestamp>
 <severity>ERROR</severity>
 <application-id>xxxxx</application-id>
 <subsystem-id>INTF</subsystem-id> 
<host-name>xxxx</host-name>
 <log-origin>PFL</log-origin>
 <thread-id>MCulEngine14</thread-id> 
<uid>606</uid> 
<message>cannot make thumbnail image</message>
 <user-id> </user-id> 
<additional-info> </additional-info>
</log-message>

Can any one help me on this.

OUTPUT should be on below manner:

 <timestamp>2019-03-13T04:52:49.648-05:00</timestamp>
<severity>ERROR</severity>
<message>cannot make thumbnail image</message>

 <timestamp>2019-03-13T04:52:49.648-05:00</timestamp>
<severity>ERROR</severity>
<message>cannot make thumbnail image</message>

Thanks in Advance,

Hello ravi,

Could you please try following once.

awk -F"[><]" '
/\/log-message/{
  if(value){
    print value
  }
  value=""
  next
}
/timestamp/{
  value="timestamp="$3
  next
}
/severity/{
  value=value ORS "severioty="$3
  next
}
/message/{
  value=value ORS "message="$3
}
END{
  if(value){
    print value
  }
}
'    Input_file

Thanks,
R. Singh

1 Like
awk '
/ERROR/                 {print prev ORS $0; flag=1}
/message/ && flag       {print; flag=0}
                        {prev = $0}
' file > out.log
1 Like

Hi Singh, Thanks for your help, the above code is giving the out put but i need out put which are related to severity=ERROR and message = cannot make thumbnail image .. these two conditions need to satisfy ..

Thanks.

Hello ravi,

2 things here.

1- For thanking any person for any useful post(s) you could HIT thanks button at bottom left corner of each post :b:
2- For your question; you have to provide samples of input and output in correct needed manner on forums, we can't guess that; so please provide in your post with CODE TAGS(see how your first post was edited).

Thanks,
R. Singh

1 Like

hi Singh, this first time am using this forum, so am not aware to click that thanks button, because of that i conveyed my greetings in words, going forward i will do HIT thanks button

I have updated my requirement input and output. thanks for your suggestion. Please help me on this.

With your amended spec, try also

grep -E "<(timestamp|severity|message)" file | paste -sd"\t\t\n" | grep -E "ERROR.*thumbnail" | tr '\t' '\n'
2 Likes

thanks for your help, its not working and not giving any output.

It has been tested with your input sample:

grep -E "<(timestamp|severity|message)" file | paste -sd"\t\t\n" | grep -E "ERROR.*thumbnail" | tr '\t' '\n'
 <timestamp>2019-03-13T04:52:49.648-05:00</timestamp>
 <severity>ERROR</severity>
<message>cannot make thumbnail image</message>

What are your OS and grep versions?

1 Like

hi RudiC, my OS is RHEL 7.5, can you please share complete code , i think some where i am missing

That IS the complete code, running with your input sample. Did you replace "file" with your input file name?

1 Like

hi RudiC, thanks for your support, issue resolved, need small info what -sd will do in your code..?

man paste :

HOW did you resolve the issue?

1 Like

gnu version

grep -B1 -A7 "ERROR" file | grep "<timestamp\|<severity\|<message"

Hi Rudi, i have made your code in shell script as below

daslog.sh

grep -E "<(timestamp|severity|message)" das.xml | paste -sd"\t\t\n" | grep -E "ERROR" | tr '\t' '\n' >> dasoutput.log

and set up cronjob like

 * * * * * /local/flamingo/log/das/daslog.sh >> /local/flamingo/log/das/output.log >> /local/flamingo/log/das/output.log

here the problem is if i run shell script manually it is updating dasoutput.log but through cronjob its not updating dasoutput.log.

can you help me on this.

--- Post updated at 07:28 AM ---

hi all,
i have made below code in shell script as below

daslog.sh
grep -E "<(timestamp|severity|message)" das.xml | paste -sd"\t\t\n" | grep -E "ERROR" | tr '\t' '\n' >> dasoutput.log

and set up cronjob like

          • /local/flamingo/log/das/daslog.sh >> /local/flamingo/log/das/output.log >> /local/flamingo/log/das/output.log

here the problem is if i run shell script manually it is updating dasoutput.log but through cronjob its not updating dasoutput.log.

can any one help me on this.

That cron job looks suspect. Do you really want it to trun every minute? Do you really need to redirect stdout twice? Do you receive an error mail?

And, the redirection in daslog.sh will override the redirection in crontab . Look for dasoutput.log in the user's home directory.

1 Like

no for testing , i made it for every minute and i tried with

          • /local/flamingo/log/das/daslog.sh > /local/flamingo/log/das/output.log >> /local/flamingo/log/das/output.log this way also but not working.

Please answer ALL questions.

1 Like