I have a small data set in an XML format:
<symbolgroupdef id="bin_11-QQQQ">
<symbol>QQQ</symbol>
</symbolgroupdef>
<symbolgroupdef id="bin_6-AAPL">
<symbol>AAPL</symbol>
</symbolgroupdef>
<symbolgroupdef id="bin_7-BIDU">
<symbol>BIDU</symbol>
</symbolgroupdef>
<symbolgroupdef id="bin_7">
<symbol>AAPL</symbol>
<symbol>IBM</symbol>
</symbolgroupdef>
I want to print out the symbolgroupdef
and the symbol
where a certain symbol exists. The symbol may appear under several symbolgroupdef
groups.
Here is the code I have written so far:
#!/usr/bin/perl
use warnings;
use strict ;
$symbol = $ARGV[0] ;
my $sym_file = "/data/xmlconfig/config.xml";
open my $sym_fh, '<', $sym_file or die $!;
while($line = <$sym_fh>) {
if (my $line =~ /\<symbolgroupdef id=\".*\"\>/) {
print $line ;
sleep 1;
}
}
Basically what I want is something with will find the symbolsgroupdef id line, look for the specified symbol under it, and if it finds it, print the symbolgroupdef is line and the symbol under it. The symbol will be a command line entry and specified by $ARGV[0]
in the above case theses two lines should be printed
<symbolgroupdef id="bin_6-AAPL">
<symbol>AAPL</symbol>
<symbolgroupdef id="bin_7">
<symbol>AAPL</symbol>
I don't have any modules on this machine, and can't install any on this machine. Please forgive me for parsing XML without a module.