I once wrote a generic XML scanner which produces output similar to what you want. It produces columns from tags in a generic way without hardcoding tags/attributes. It has a weakness in that it can't handle spaces inside tag attributes.
Getting those two 'env' tags into one can be done with sed.
Code:
$ cat xmlg.awk
BEGIN { RS="<"; FS=">"; ORS="\r\n";
# Change this to alter how many close-tags in a row are needed
# before a row of data is printed.
DEP=1
SEP="\t"
}
# Skip weird XML specification lines or blank records
/^\?/ || /^$/ { next }
# Handle close tags
/^[/]/ {
N=D; while((N>0) && ("/"STACK[N] != $1)) N--;
if("/"STACK[N] == $1) D=(N-1);
POP++;
if(POP == DEP)
{
if(!HEADER++)
{
split(ARG[1], Z, SUBSEP);
printf("%s %s", Z[2], Z[3]);
for(N=2; N<=ARG_; N++)
{
split(ARG[N], Z, SUBSEP);
printf("%s%s %s", SEP, Z[2], Z[3]);
}
printf("\n");
}
printf("%s", DATA[ARG[1]]);
for(N=2; N<=ARG_; N++)
printf("%s%s", SEP, DATA[ARG[N]]);
printf("\n");
}
next
}
# Handle open tags
{
gsub(/^[ \r\n\t]*/, "", $2); # Whitespace isn't data
gsub(/[ \r\n\t]*$/, "", $2);
sub(/\/$/, "", $(NF-1));
# Reset parameters
POP=0;
M=split($1, A, " ");
STACK[++D]=A[1];
if((!MAX) || (D>MAX)) MAX=D; # Save max depth
# Handle parameters
Q=split(A[2], B, " ");
for(N=1; N<=Q; N++)
{
split(B[N], C, "=");
gsub(/['"]/,"", C[2]);
I=D SUBSEP STACK[D] SUBSEP C[1];
if(!SEEN[I]++)
ARG[++ARG_]=I;
DATA[I]=C[2];
}
if($2)
{
I=D SUBSEP STACK[D] SUBSEP "CDATA";
if(!SEEN[I]++)
ARG[++ARG_]=I;
DATA[I]=$2;
}
}
$ sed 's/env="\([^"]*\)" env="\([^"]*\)"/env="\1\2"/g' 3.xml | awk -f xmlg.awk
rel ver mod name node env ins ip ins ip
123 on ac1 10.192.0.1 10.192.0.2
123 on ac2 10.192.0.3 10.192.0.4
123 on pr 10.192.0.5 10.192.0.6
123 off ac1 10.192.0.7 10.192.0.6
123 off ac2 10.192.0.8 10.192.0.6
123 off pr 10.192.0.9 10.192.0.6
$
Hi All,
I am new to Shell scripting.
I have a log file containing XML Messages.Each XML Message is accompanied with a timestamp.I need to count the the number of messages that get logged in a particular timeinterval.Is there any command/Syntax to achieve this.
Any code/example is... (5 Replies)
Hi all
Is there a way in awk to know that you are processing your final line of input if you do no know how many lines were in the input to begin with?
Thanks (7 Replies)
Hi,
i am really fresh with shell scripting and programming,
i have an issue i am not able to solve to populate data on my server for Cisco IP phones.
I have CSV file within the following format:
;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;... (9 Replies)
Hello,
I trying to extract text that is surrounded by xml-tags. I tried this
cat tst.xml | egrep "<SERVER>.*</SERVER>" |sed -e "s/<SERVER>\(.*\)<\/SERVER>/\1/"|tr "|" " "
which works perfect, if the start-tag and the end-tag are in the same line, e.g.:
<tag1>Hello Linux-Users</tag1>
... (5 Replies)
Hi everyone,
I have Xml files in a folder, I need to extract some attribute values form xml files and store in a hash. My xml file look like this.
<?xml version="1.0" encoding="UTF-8"?>
<Servicelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"... (0 Replies)
I need to get all session_ID 's for product="D-0002" from a XML file:
Sample input:
<session session_ID="6411206" create_date="2012-04-10-10.22.13.000000">
<marketing_info>
<program_id>D4AWFU</program_id>
<subchannel_id>abc</subchannel_id>
</marketing_info>
... (1 Reply)
Hello,
I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this :
This is the output of ls command : I stored the output in a file filelist
1.1M... (5 Replies)
Hi ,
I have a below xml:
<ns:Body>
<ns:result>
<Date Month="June" Day="Monday:/>
</ns:result>
</ns:Body>
i have a lookup abc.txtt text file with below details
Month June July August
Day Monday Tuesday Wednesday
I need a output xml with below tags
<ns:Body>
<ns:result>... (2 Replies)
I've been kicking this around for a while now, I might as well post it here.
v0.0.9, now properly supporting self-closing tags.
v0.0.8, an important quoting fix and a minor change which should handle special <? <!-- etc. tags without seizing up as often. Otherwise the code hasn't changed much.... (6 Replies)
Discussion started by: Corona688
6 Replies
LEARN ABOUT DEBIAN
mkdoc::xml::stripper
MKDoc::XML::Stripper(3pm) User Contributed Perl Documentation MKDoc::XML::Stripper(3pm)NAME
MKDoc::XML::Stripper - Remove unwanted XML / XHTML tags and attributes
SYNOPSIS
use MKDoc::XML::Stripper;
my $stripper = new MKDoc::XML::Stripper;
$stripper->allow (qw /p class id/);
my $ugly = '<p class="para" style="color:red">Hello, <strong>World</strong>!</p>';
my $neat = $stripper->process_data ($ugly);
print $neat;
Should print:
<p class="para">Hello, World!</p>
SUMMARY
MKDoc::XML::Stripper is a class which lets you specify a set of tags and attributes which you want to allow, and then cheekily strip any
XML of unwanted tags and attributes.
In MKDoc, this is used so that editors use structural XHTML rather than presentational tags, i.e. strip anything which looks like a <font>
tag, a 'style' attribute or other tags which would break separation of structure from content.
DISCLAIMER
This module does low level XML manipulation. It will somehow parse even broken XML and try to do something with it. Do not use it unless
you know what you're doing.
API
my $stripper = MKDoc::XML::Stripper->new()
Instantiates a new MKDoc::XML::Stripper object.
$stripper->load_def ($def_name);
Loads a definition located somewhere in @INC under MKDoc/XML/Stripper.
Available definitions are:
xhtml10frameset
xhtml10strict
xhtml10transitional
mkdoc16 - MKDoc 1.6. XHTML structural markup
You can also load your own definition file, for instance:
$stripper->load_def ('my_def.txt');
Definitions are simple text files as follows:
# allow p with 'class' and id
p class
p id
# allow more stuff
td class
td id
td style
# etc...
$stripper->allow ($tag, @attributes)
Allows "<$tag>" to appear in the stripped XML. Additionally, allows @attributes to appear as attributes of <$tag>, so for instance:
$stripper->allow ('p', 'class', 'id');
Will allow the following:
<p>
<p class="foo">
<p id="bar">
<p class="foo" id="bar">
However any extra attributes will be stripped, i.e.
<p class="foo" id="bar" style="font-color: red">
Will be rewritten as
<p class="foo" id="bar">
$stripper->disallow ($tag)
Explicitly disallows a tag and all its associated attributes. By default everything is disallowed.
$stripper->process_data ($some_xml);
Strips $some_xml according to the rules that were given with the allow() and disallow() methods and returns the result. Does not modify
$some_xml in place.
$stripper->process_file ('/an/xml/file.xml');
Strips '/an/xml/file.xml' according to the rules that were given with the allow() and disallow() methods and returns the result. Does not
modify '/an/xml/file.xml' in place.
NOTES
MKDoc::XML::Stripper does not really parse the XML file you're giving to it nor does it care if the XML is well-formed or not. It uses
MKDoc::XML::Tokenizer to turn the XML / XHTML file into a series of MKDoc::XML::Token objects and strictly operates on a list of tokens.
For this same reason MKDoc::XML::Stripper does not support namespaces.
AUTHOR
Copyright 2003 - MKDoc Holdings Ltd.
Author: Jean-Michel Hiver
This module is free software and is distributed under the same license as Perl itself. Use it at your own risk.
SEE ALSO
MKDoc::XML::Tokenizer MKDoc::XML::Token
perl v5.10.1 2004-10-06 MKDoc::XML::Stripper(3pm)