Guest User

Untitled

a guest
Jan 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. $ cat file_<ID>.xml
  2. ...
  3. ...
  4. ...
  5. <double>1.2342</double>
  6. <double>2.3456</double>
  7. ...
  8. ...
  9. ...
  10. ...
  11.  
  12. file_0001 1.2342 2.3456 ...
  13. file_0002 1.2342 2.3456 ...
  14.  
  15. ./from_xml_to_csv.sh 100.45s user 94.84s system 239% cpu 1:21.48 total
  16.  
  17. find . ! -name . -prune -name pattern -type f -exec cat {} + | ...
  18.  
  19. #!/usr/bin/env perl
  20.  
  21. use strict;
  22. use warnings;
  23. use XML::Twig;
  24.  
  25. my $twig = XML::Twig -> new;
  26. $twig -> parse ( *DATA );
  27.  
  28. foreach my $double ( $twig -> get_xpath('//double') ) {
  29. print $double -> trimmed_text,"n";
  30. }
  31.  
  32. __DATA__
  33. <root>
  34. <subnode>
  35. <another_node>
  36. <double>1.2342</double>
  37. <double>2.3456</double>
  38. <some_other_tag>fish</some_other_tag>
  39. </another_node>
  40. </subnode>
  41. </root>
  42.  
  43. 1.2342
  44. 2.3456
  45.  
  46. #!/usr/bin/env perl
  47.  
  48. use strict;
  49. use warnings;
  50. use XML::Twig;
  51. use Text::CSV;
  52.  
  53. my $twig = XML::Twig->new;
  54. my $csv = Text::CSV->new;
  55.  
  56. #open our results file
  57. open( my $output, ">", "results.csv" ) or die $!;
  58. #iterate each XML File.
  59. foreach my $filename ( glob("/path/to/xml/*.xml") ) {
  60. #parse it
  61. $twig->parsefile($filename);
  62. #extract all the text of all the 'double' elements.
  63. my @doubles = map { $_->trimmed_text } $twig->get_xpath('//double');
  64. #print it as comma separated.
  65. $csv->print( $output, [ $filename, @doubles ] );
  66.  
  67. }
  68. close($output);
  69.  
  70. get_xpath("/root/subnode/another_node/double")
  71.  
  72. for f in `ls *.xml` ;
  73. do
  74. echo $f,`grep double $f | awk -F '[<>]' '{print $3}' | tr 'n' ','`;
  75. done
Add Comment
Please, Sign In to add comment