Link to home
Start Free TrialLog in
Avatar of gauravflame
gauravflame

asked on

File Parsing - Perl

10lines.txt
123
123
123
123
123
123
123
123

1234
# Comment line
1234
# Comment line
1234

Question ::

How to read last 3 lines and transfer to new file with
1) Removing any blank lines
2) Removing any lines start with "#"

Above 3 lines should not be counted



code::
===============================

use Data::Dumper;
$data_file = 'C:\10lines.txt';

open(DAT, $data_file) || die("Could not open file!");
@raw_data=<DAT>;

foreach $print (@raw_data)
{
   $print = ~ s/#.*//;
   $print = s/^\s+$//;


}

print Dumper @row;
#print @raw_data, scalar @raw_data;
#print Dumper @raw_data;
open NEW,">C:/3lines.txt";
#print NEW @raw_data[-3..-1];
print NEW @raw_data[-3..-1] or die$!;
close NEW or die $!;
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gauravflame
gauravflame

ASKER

What this      &&!    means inside the grep
#!/usr/bin/perl -w
use strict;

open (IN, "C:/10lines.txt") or die "Could not open 10lines.txt: $!\n";
open (OUT, ">C:/3lines.txt") or die "Could not open 3lines.txt: $!\n";

while (<IN>) {
    print OUT unless (/^#/ or /^\s*$/);
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
      Binary "&&" performs a short-circuit logical AND operation.  That is,
       if the left operand is false, the right operand is not even evaluated.
       Scalar or list context propagates down to the right operand if it is
       evaluated.

       Unary "!" performs logical negation, i.e., "not".  See also "not" for a
       lower precedence version of this.
Hi ozo ,

I like to make sure with you

grep/\S/&&!/^#/

means here "Take out Blank lines &&! (And not only those lines but also the lines  start with the )  #

This make sense or something else ! :) .. my theory !




Hi mjcoyne

can you explain your code .. how you are extracting last 3 lines
take only lines containing a non-blank character and not starting with #
#!/usr/bin/perl -w
use strict;

# declare variable to hold the contents of the input file.

my $in;

# open the input and output files.

open (IN, "C:/10lines.txt") or die "Could not open 10lines.txt: $!\n";
open (OUT, ">C:/3lines.txt") or die "Could not open 3lines.txt: $!\n";

# "slurp" the input file into the $in variable.  $in now contains the entire
# contents of 10lines.txt.

{ local $/; $in = <IN>; }

# this regular expression says to replace everything up to and including
# the first empty line (detected by seeing two end-of-line characters
# in a row) with nothing (which means the same as "delete"), and/or
# to similarly replace (delete) all lines beginning with a pound sign.
#
# the switches at the end (sg) say to allow the dot character to match
# end-of-line chararacters (e.g. \n), and instructs the regular expression
# to act "globally" (i.e. run the regular expression against the whole file.

$in =~ s/(.+\n\n)|(#.+?\n)//sg;

# if you remove from the data you provided (contained in 10lines.txt,
# and subsequently held by $in) everything up to and including the
# first blank line and all lines beginning with a pound sign, all that's
# left is the three 1234 lines. Since this is what we wanted, print
# the now-correct contents of $in to the file pointed at by the
# filehandle OUT

print OUT $in;
If you want all uncommented nonblank lines following the first blank line:

$data_file = 'C:/10lines.txt';
open(DAT, $data_file) || die("Could not open $data_file  $!");
while( <DAT> ){
   print if (!/\S/..0)&&/\S/&&!/^#/;
}