Обсуждение: Deleting rows in a file based on condition

Поиск
Список
Период
Сортировка

Deleting rows in a file based on condition

От
Ken Hill
Дата:
I have the following perl script that reads a fixed-width file and replaces values in various sections of the file.<br
/><br/> -------------------------------------------<br /> open (IN, '< in.txt');<br /> open (OUT, '>
out_test.txt');<br/><br /> while (<IN>) {<br /><br />           chomp;<br /><br />           $first_section =
substr$_, 0, 381;     # extract the first section of the record<br />           $facilityno = substr $_, 381, 10;    #
extractthe facilityno field<br />           $second_section = substr $_, 391, 1056;        # extract the second section
ofthe record<br />           $requestor_section=" " x 500;<br />   # $requestor_section = substr $_, 1446, 499; #
extractrequestor section of record<br />           $third_section = substr $_, 1946, 4748; # extract third section of
record<br/>           <br />           # print out the file with changed facilityno value ...<br /><br />          
printOUT "$first_section$\0039007300$\$second_section$\$requestor_section$\$third_section\n";<br />           <br />
}<br/><br /> close (IN);<br /> close (OUT);<br /> ------------------------------------------------<br /><br /> I want
toplace an "if...then" condition on the $facilityno value; such that if the $facilityno value = 0000000000, delete the
record(e.g., don't print out that row); rather skip that row and continue printing out the remaining rows.<br /><br />
Anyadvice is very much appreciated.<br /><br /> 

Re: Deleting rows in a file based on condition

От
Ken Hill
Дата:
Oops. I posted this to the wrong support list. Sorry.<br /><br /> -Ken<br /><br /> On Fri, 2006-02-10 at 09:52 -0800,
KenHill wrote:<br /><blockquote type="CITE"><font color="#000000">I have the following perl script that reads a
fixed-widthfile and replaces values in various sections of the file.</font><br /><br /><font
color="#000000">-------------------------------------------</font><br/><font color="#000000">open (IN, '<
in.txt');</font><br/><font color="#000000">open (OUT, '> out_test.txt');</font><br /><br /><font
color="#000000">while(<IN>) {</font><br /><br /><font color="#000000">          chomp;</font><br /><br /><font
color="#000000">         $first_section = substr $_, 0, 381;     # extract the first section of the record</font><br
/><fontcolor="#000000">          $facilityno = substr $_, 381, 10;    # extract the facilityno field</font><br /><font
color="#000000">         $second_section = substr $_, 391, 1056;        # extract the second section of the
record</font><br/><font color="#000000">          $requestor_section=" " x 500;</font><br /><font color="#000000">  #
$requestor_section= substr $_, 1446, 499; # extract requestor section of record</font><br /><font
color="#000000">         $third_section = substr $_, 1946, 4748; # extract third section of record</font><br /><font
color="#000000">         </font><br /><font color="#000000">          # print out the file with changed facilityno
value...</font><br /><br /><font color="#000000">          print OUT
"$first_section$\0039007300$\$second_section$\$requestor_section$\$third_section\n";</font><br/><font
color="#000000">         </font><br /><font color="#000000">}</font><br /><br /><font color="#000000">close
(IN);</font><br/><font color="#000000">close (OUT);</font><br /><font
color="#000000">------------------------------------------------</font><br/><br /><font color="#000000">I want to place
an"if...then" condition on the $facilityno value; such that if the $facilityno value = 0000000000, delete the record
(e.g.,don't print out that row); rather skip that row and continue printing out the remaining rows.</font><br /><br
/><fontcolor="#000000">Any advice is very much appreciated.</font><br /><br /></blockquote>