[SOLVED] extract directory and xml data to create a comma delimited file
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Actually, I don't think you need to make it that complex. The parent tag appears to be unique, and there's only one "value" in it, so it should be quite simple to grab.
Code:
xmlparts=$( xml sel -t -v '//hudson.model.StringParameterValue/value' -o , -v /build/result -o , -v '/build/culprits/string[last()]' build.xml "$dir/$file" )
Using two slashes in front of an element appears to make the matching "global", bounded only by any higher level paths specified in front of it. In any case you can always provide the full tree path to the exact entry you want.
Now do we have everything?
Edit: BTW, it looks like the only reason your last command is failing is because it uses single quotes rather than doubles. Since the outer quotes are also single, It means that they pair up with them instead and leave the tagName string unprotected by the shell. So no quotes actually get passed to the command.
Last edited by David the H.; 05-09-2012 at 01:04 PM.
returns an error.
Entity: line 23: parser error : Couldn't find end of Start Tag value-of line 23
udson.model.ParametersAction/parameters/hudson.model.StringParameterValue[name="
thus why I went with the [name='tagName'] and it worked with [name='tagName'] until I tried to include the /description ... I think the brackets make the single quotes "local".
I believe now that provides what the requirements were. Thank you so very much.
returns an error.
Entity: line 23: parser error : Couldn't find end of Start Tag value-of line 23
udson.model.ParametersAction/parameters/hudson.model.StringParameterValue[name="
Older versions of xmlstarlet (1.0.x and earlier) create an XSLT document as a string and then parse it, double quotes are special in XML so the parser gets tripped up (1.0.4 and later will escape these args).
XML files should always be processed using XML tools, like above posters have shown.
Still, if the build.xml files are as limited to the format shown in the example, then it is certainly possible to parse them with plain awk . I would personally consider this only if using the proper tools was too slow or burdensome.
Anyway, here is the awk script:
Code:
#!/usr/bin/awk -f
BEGIN {
RS = "[\t\n\v\f\r ]*<"
FS = "[\t\n\v\f\r ]*>[\t\n\v\f\r ]*"
element = "" # XML element name
content = "" # Immediate content to element
isopen = 0
parents = 0 # Number of parent elements
parent[0] = "" # Current element
parent[1] = "" # Parent element to current element
}
#
# Per-file initialization
#
(FNR == 1) {
# Per-file initialization.
# Current file name (and path) is in FILENAME.
result = ""
description = ""
timestamp = ""
basename = FILENAME
sub(/^.*\/Deploy_/, "Deploy_", basename)
split(basename, path, "/")
}
#
# XML processing
#
{
if (isopen) {
isopen = 0
if (length(element) > 0) {
for (i = parents; i >= 0; i--)
parent[i+1] = parent[i]
parents++
}
}
if (NF < 1)
next
if ($1 ~ /^[!?]/)
next
if (NF > 2) {
printf("Spurious > after %s.\n", $2) > "/dev/stderr"
exit(1)
}
element = $1
content = $2
sub(/^[\t\n\v\f\r ]+/, "", content)
sub(/[\t\n\v\f\r ]+$/, "", content)
# To combine all whitespace in content to single spaces, add
# sub(/[\t\n\v\f\r ]+/, " ", content)
if (element ~ /^\//) {
sub(/^\/+/, "", element)
sub(/[\t\n\v\f\r ].*$/, "", element)
if (parent[1] != element) {
printf("%s: Element not open.\n", element) > "/dev/stderr"
exit(1)
}
for (i = 1; i < parents; i++)
parent[i] = parent[i+1]
delete parent[parents]
parents--
next
}
if (element ~ /\/$/)
sub(/\/+$/, "", element)
else
isopen = 1
sub(/[\t\n\v\f\r ].*$/, "", element)
parent[0] = element
}
# Actual processing starts here. Available:
# parents The number of parent elements for current node
# parent[0] The current element name
# parent[1] The name of closest parent element
# parent[parents] The name of the root element
# content Immediate content following the element.
# Does not include content after any child elements,
# even if they do belong to the current element.
(parents == 1 && parent[1] == "build" && parent[0] == "description") {
description = content
next
}
(parents == 1 && parent[1] == "build" && parent[0] == "result") {
result = content
next
}
(parents == 2 && parent[2] == "build" && parent[1] == "culprits" && parent[0] == "string") {
printf("%s,%s,%s,%s,%s\n", path[1], path[3], description, result, content)
}
Run it using
Code:
above-script.awk jobs/*/*/*/build.xml
or, if you have too many files for a single command,
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.