shell-script-pt
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [shell-script] Filtrar XML


From: Luiz Alberto
Subject: Re: [shell-script] Filtrar XML
Date: Thu, 10 Dec 2015 15:31:32 -0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Icedove/38.4.0

On 10-12-2015 14:24, Luiz Alberto address@hidden [shell-script] wrote:
 

On 09-12-2015 15:04, Alfredo Casanova address@hidden [shell-script] wrote:
 
Provavelmente não vai funcionar pra todos os casos, mas é um caminho. 

On Wed, Dec 9, 2015 at 2:57 PM Alfredo Casanova <address@hidden> wrote:
$ cat teste
#!/bin/bash
flag=3;
print=3;
while read line; do 
[ "$line" == "<tagy>" ] && { flag=1; cache="$line"; continue; }
(( $flag == 1 )) && { 
[ "$line" == "<tagz>true</tagz>" ] && print=1;
[ "$line" == "<tagz>false</tagz>" ] && print=0;
[ "$line" == "</tagy>" ] && flag=0; 
cache+="
$line";
}
if (( $print == 1 )) && (( $flag == 0 )); then
echo "$cache"
else
continue;
fi
done < input

$ cat input
<tagx>
1
2
</tagx>
<tagy>
3
4

   <tagz>true</tagz>
5
6
</tagy>
<tagy>
7
8
   <tagz>false</tagz>
9
10
</tagy>


$ ./teste 
<tagy>
3
4

<tagz>true</tagz>
5
6
</tagy>


On Wed, Dec 9, 2015 at 1:49 PM Tiago Peczenyj address@hidden [shell-script] <address@hidden> wrote:
 
vamos la

isso é um xml, lide com isso como se fosse um xml. existem ferramentas especificas para tratar com xml e vc tem um arquivo que representa um tipo muito abstrato de informação.

existe a chance disso dar errado se vc trata xml como texto. se o formato é comportado vc ate pode lidar porém uma coisinha diferente vai te dar um resultado todo errado. acredite.

por exemplo, se vc usar o xmllint + xpath por //*[yay="true"]

$ cat a.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<foo></foo>
<bar><yay>true</yay></bar>
<baz><yay>true</yay></baz>
<bam><yay>false</yay></bam>
</root>

$ xmllint a.xml --shell
/ > cat //*[yay="true"]
 -------
<bar>
  <yay>true</yay>
</bar>
 -------
<baz>
  <yay>true</yay>
</baz>

Isso ja te da algumas ideias. infelizmente o xmllint não é muito amigavel e varias coisas não deram muito certo ( acho overkill ter q usar expect so pra mandar o comandos para ele uma vez que o --xpath retorna tudo junto 'inline'

boa sorte.

2015-12-09 16:22 GMT+01:00 César Vianna address@hidden [shell-script] <address@hidden>:
 

Boa tarde!
Tenho um arquivo XML com o seguinte padrão :

<tagx>
....
....
</tagx>
<tagy>
....
   <tagz>true</tagz>
....
</tagy>
<tagy>
....
   <tagz>false</tagz>
....
</tagy>

Preciso filtrar todos os blocos  de tagy que contenham a tagz com true.

Com grep e sed só consigo tratar linha a linha.  Acho que a saída é awk.
Alguma ideia?




--
Tiago B. Peczenyj

http://about.me/peczenyj



reply via email to

[Prev in Thread] Current Thread [Next in Thread]