LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-13-2012, 02:51 AM   #1
frambau
Member
 
Registered: Feb 2012
Posts: 32

Rep: Reputation: Disabled
Print xml tag only if lenght > 0


Hi,

I have this xml:

<item>
<title>Jack win the news</title>
<link>http://nom/hot/jack+white</link>
<guid>http://nt/jack+white</guid>
<name>juan</name>
<surname>martinez</surname>
</item>
<item>
<title></title>
<link>http://wasdfsadfw=1050681776</link>
<guid>http://fa&asadfp;id=1050681776</guid>
<name>paddco</name>
<surname>perddez</surname>
</item>
<title>Glad to roll..</title>
<link>http://ww=1050681776</link>
<guid>http://fa&amp;id=1050681776</guid>
<name>paco</name>
<surname>perez</surname>
</item>
...
...
...

and i need write all tags title in a different file index like this

Jack win the news > try_1.out
Glad to roll.. > try_3.out
...
...
...

I am using this sentence
H="try_"
awk -v name=$H 'BEGIN{RS = "[<>]"}{ORS = /title/?"\n":"|"}/item/,/\/item/{if(/^(title)$/ && getline) print>(name)(++i)".out";} input.xml

but this generate 3 files

try_1.out with this "Jack win the news"
try_2.out with a blank line
try_3.out with this "Glad to roll.."

How i can generate only the two files?


Thanks for all
 
Old 02-13-2012, 03:03 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
So implement a check of the length using the length function
 
Old 02-13-2012, 03:07 AM   #3
frambau
Member
 
Registered: Feb 2012
Posts: 32

Original Poster
Rep: Reputation: Disabled
thanks grail so how????

I'am trying this

awk -v name=$H 'BEGIN {RS = "[<>]"}
{ORS = /description/?"\n":"|"}/item/,/\/item/{if((/^(description)$/ && getline)!="")print > (name)(++i)".out"; }
' GGITBYZK5ThI7gtsU6wydg.xml

but this print a lot of archives, because i think that this print all lines.
 
Old 02-13-2012, 04:05 AM   #4
frambau
Member
 
Registered: Feb 2012
Posts: 32

Original Poster
Rep: Reputation: Disabled
Thanks,

I solve my question with this.

awk -v prefix="try_" '
BEGIN {
RS = "[\t\n\v\f\r ]*[<]"
FS = "[>][\t\n\v\f\r ]*"
id = 0
}

($1 == "item" || $1 ~ /^item[\t\n\v\f\r ]/) {
split("", field)
id++
next
}

($1 ~ /^[A-Za-z]/) {
name = $1
sub(/[\t\n\v\f\r ]*$/, "", name)
field[name] = $2
next
}

($1 == "/item") {
if(field["description"] != "")
{
print field["description"] > "try_"(++i)".out";

}
else
{
i++
}
}

' input_file
 
Old 02-13-2012, 04:48 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
I probably would have just added an if prior to you print, something like:
Code:
awk -v name=$H 'BEGIN{RS = "[<>]"}{ORS = /title/?"\n":"|"}/item/,/\/item/{if(/^(title)$/ && getline && length) print>(name)(++i)".out";}' input.xml
 
Old 02-13-2012, 05:10 AM   #6
frambau
Member
 
Registered: Feb 2012
Posts: 32

Original Poster
Rep: Reputation: Disabled
thanks again Grail.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding new tag to an xml file using sed nano2 Linux - General 6 11-23-2011 05:47 AM
how to find the end tag in xml file. mariakumar Linux - General 1 12-21-2010 10:12 AM
How To get the data from a tag in XML File kingmaker2003 Programming 7 12-04-2008 11:12 PM
XSLT/xml enforcing full tag syntax bigearsbilly Programming 2 11-23-2005 02:38 AM
Parsing XML tags with php, can't get attributes of a tag jimieee Programming 1 05-05-2004 10:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration