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>
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.