• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

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 $!;
0
gauravflame
Asked:
gauravflame
  • 4
  • 3
  • 3
2 Solutions
 
ozoCommented:
print NEW (grep/\S/&&!/^#/,@raw_data)[-3..-1] ;
0
 
gauravflameAuthor Commented:
What this      &&!    means inside the grep
0
 
mjcoyneCommented:
#!/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*$/);
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
mjcoyneCommented:
I may have misunderstood the question -- I thought you wanted to print everything but blank or comment lines.  Now that I re-read the question, I see you want to print the last three lines only, eliminating the blank lines and the comment lines.  Try this:

#!/usr/bin/perl -w
use strict;

my $in;

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";

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

$in =~ s/(.+\n\n)|(#.+?\n)//sg;
print OUT $in;
0
 
ozoCommented:
      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.
0
 
gauravflameAuthor Commented:
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 !




0
 
gauravflameAuthor Commented:
Hi mjcoyne

can you explain your code .. how you are extracting last 3 lines
0
 
ozoCommented:
take only lines containing a non-blank character and not starting with #
0
 
mjcoyneCommented:
#!/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;
0
 
ozoCommented:
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/&&!/^#/;
}

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now