Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 2.78 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Read a large file and output sections matching multiple parameters
  2. use strict;
  3. use warnings;
  4.  
  5. my @record;
  6. my %f;
  7. while(<DATA>) {
  8.     if (/^#START / .. /^#END */) {
  9.         if (/^#START /) {
  10.             @record = (); # reset
  11.             %f = ();
  12.         }
  13.         push @record, $_;
  14.         if (/^#END */) { # check and print
  15.             if ($f{'LOCATION'} !~ /$f{'MyField'}/) {
  16.                 print @record;
  17.             }
  18.         } else {         # add fields to hash
  19.             if (/^#FIELD (.+)/) {
  20.                             # use split with limit of 2 fields
  21.                 my ($key, $val) = split /=/, $1, 2;
  22.                 next unless $val; # no empty values
  23.                 $val =~ s/^"|"$//g; # strip quotes
  24.                 $f{$key} = $val;
  25.             }
  26.         }
  27.     }
  28. }
  29.  
  30. __DATA__
  31. #START Descriptor
  32. #FIELD LOCATION="http://path.to/file/here&Value=FOO&OtherValue=BLAH"
  33. #FIELD AnythingElse
  34. #FIELD MyField="BAR"
  35. #END
  36. #START Descriptor
  37. #FIELD LOCATION=http://path.to/file/here&Value=BAR&OtherValue=BLAH"
  38. #FIELD AnythingElse
  39. #FIELD MyField="BAR"
  40. #END
  41.        
  42. perl -ne 'BEGIN { $/ = "#ENDn" }' -e '/MyField="(.*?)"/; print if !/Value=$1/' <file >newfile
  43.        
  44. gawk '
  45. {
  46.     if ($2!~/^#FIELD LOCATION/)
  47.     {
  48.         next;
  49.     }
  50.     else
  51.     {
  52.         split($2,ary,"=|&");
  53.         split($4,ary1,"=|"");
  54.         if(ary[4]!=ary1[3])
  55.             {
  56.                 print $0 > "badrec.file"
  57.             }
  58.     }
  59. }' RS="#ENDn" ORS="#ENDn" FS="n" file
  60.        
  61. [jaypal:~/Temp] cat file
  62. #START Descriptor # Good Record
  63. #FIELD LOCATION="http://path.to/file/here&Value=BAR&OtherValue=BLAH"
  64. #FIELD AnythingElse
  65. #FIELD MyField="BAR"
  66. #END
  67. #START Descriptor # Bad Record
  68. #FIELD LOCATION="http://path.to/file/here&Value=FOO&OtherValue=BLAH"
  69. #FIELD AnythingElse
  70. #FIELD MyField="BAR"
  71. #END
  72. #START Descriptor # Good Record
  73. #FIELD LOCATION="http://path.to/file/here&Value=BAR&OtherValue=BLAH"
  74. #FIELD AnythingElse
  75. #FIELD MyField="BAR"
  76. #END
  77.        
  78. [jaypal:~/Temp] gawk '
  79. {
  80.     if ($2!~/^#FIELD LOCATION/)
  81.     {
  82.         next;
  83.     }
  84.     else
  85.     {
  86.         split($2,ary,"=|&");
  87.         split($4,ary1,"=|"");
  88.         if(ary[4]!=ary1[3])
  89.             {
  90.                 print $0 > "badrec.file"
  91.             }
  92.     }
  93. }' RS="#ENDn" ORS="#ENDn" FS="n" file
  94.  
  95. [jaypal:~/Temp] cat badrec.file
  96. #START Descriptor # Bad Record
  97. #FIELD LOCATION="http://path.to/file/here&Value=FOO&OtherValue=BLAH"
  98. #FIELD AnythingElse
  99. #FIELD MyField="BAR"
  100. #END
  101.        
  102. #!/usr/bin/perl
  103.  
  104. $/ = "#ENDn";
  105.  
  106. while (<DATA>) {
  107.     next unless /^#FIELD LOCATION/m;
  108.     /^#FIELD MyField="(.*)"$/m;
  109.     next if /^#FIELD LOCATION.*$1/m;
  110.     print
  111. }
  112.  
  113.  
  114.  
  115. __DATA__
  116. #START Descriptor
  117. #FIELD LOCATION="http://path.to/file/here&Value=FOO&OtherValue=BLAH"
  118. #FIELD AnythingElse
  119. #FIELD MyField="BAR"
  120. #END
  121. #START Descriptor
  122. #FIELD LOCATION=http://path.to/file/here&Value=BAR&OtherValue=BLAH"
  123. #FIELD AnythingElse
  124. #FIELD MyField="BAR"
  125. #END