Link to home
Create AccountLog in
Avatar of gauravflame
gauravflame

asked on

File Extraction :: 10 to 3 lines

Text File ::  total 10 lines :: 10lines.txt

abc|xyz|pqr
abc|xyz|pqr
abc|xyz|pqr
abc|xyz|pqr
abc|xyz|pqr
abc|xyz|pqr
abc|xyz|pqr
abc|xyz|pqr
abc|xyz|pqr
abc|xyz|pqr


Code::

$data_file = 'C:\10lines.txt';
open(DAT, $data_file) || die("Could not open file!");
@raw_data=<DAT>;
#$array = [];

#$j =0;
foreach $wrestler (@raw_data)
{
#$array->[$j] = $wrestler;


#$j++;
}

close(DAT);

Question ::

How to extract last 3 lines from the 10lines.txt file without hardcording and then transfer it into the new
textfile 3lines.txt



Avatar of ozo
ozo
Flag of United States of America image

open NEW,">3lines.txt" or die "3lines.txt $!";
print NEW @raw_data[-3..-1];
close NEW;
Avatar of gauravflame
gauravflame

ASKER

I like to retain the old file and create the new file
======================

$data = 'c:\10lines.txt';
@raw_data = ();
open NEW,">$data" ;
print NEW @raw_data[-3..-1];   # Error use of uninitial value
close NEW;

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Getting Error  :: Second last line

$data_file = 'C:\10lines.txt';
open(DAT, $data_file) || die("Could not open file!");
@raw_data=<DAT>;
open NEW,">3lines.txt" or die "3lines.txt $!";
print NEW @raw_data[-3..-1];   # Use of uninitialize in print
close NEW;
That means that there were less than 3 lines in @raw_data
right now  code is working fine but not able to create 3lines.txt file.

10lines.txt file has 10 lines , I make sure it
What do you see from
print @raw_data, scalar @raw_data;
o/p

abc|xyz|prc
abc|xyz|prc
abc|xyz|prc
abc|xyz|prc
abc|xyz|prc
abc|xyz|prc
abc|xyz|prc
abc|xyz|prc
abc|xyz|prc
abc|xyz|prc
10
Can you post the entire program and any error messages you get
I am not getting any error able to print last line "Done"

$data_file = 'C:\10lines.txt';
open(DAT, $data_file) || die("Could not open file!");
@raw_data=<DAT>;
print @raw_data, scalar @raw_data;
#print Dumper @raw_data;
open NEW,">3lines.txt" or die "3lines.txt $!";
print NEW @raw_data[-3..-1];
close NEW;
close DAT;
print "\n Done ";
last 3 lines?

open (FILE,"file") || die "cannot open";
$last3="";
$last2="";
$last1="";
while ( my $line = <FILE>) {
    chomp($line);
    $last3 = $last2;
    $last2 = $last1;
    $last1 = $line;
}
print $last3."\n" ;
print $last2."\n";
print $last1."\n";
Then what  is the problem?
What is in 3lines.txt after running that program?
Do you get any errors from
print NEW @raw_data[-3..-1] or dire$!;
close NEW or die $!;
things become complex with last 20 lines
Do you get any errors from
print NEW @raw_data[-3..-1] or die $!;
close NEW or die $!;
ozo,
I am not getting any error messages not even warning  , but not able to create new file 3lines.txt
I used the die not dire!
Where are you looking for 3lines.txt?
What if you do
   open NEW,">C:/3lines.txt" or die "C:/3lines.txt $!";
Did anything happen when you ran the your program
  $data = 'c:\10lines.txt';
  @raw_data = ();
  open NEW,">$data" ;
  print NEW @raw_data[-3..-1];   # Error use of uninitial value
  close NEW;
(it should have created an empty c:\10lines.txt)
Where are you looking for 3lines.txt?

Ans :: under    C:\ Drive

What if you do
   open NEW,">C:/3lines.txt" or die "C:/3lines.txt $!";
Did anything happen when you ran the your program
  $data = 'c:\10lines.txt';
  @raw_data = ();
  open NEW,">$data" ;
  print NEW @raw_data[-3..-1];   # Error use of uninitial value
  close NEW;
(it should have created an empty c:\10lines.txt)
back to top

Ans :: I got the empty file, again I filled it with charaters
your code works fine!
the 3lines.txt file will be created in the directory where you ran the perl script. check to make sure its there...
Was C:\ Drive the current working directory when you ran the program?
What if you do
 open NEW,">C:/3lines.txt" or die "C:/3lines.txt $!";
or
 open NEW,">C:/ Drive/3lines.txt" or die "C:/ Drive/3lines.txt $!";
I got it with

open NEW,">C:/3lines.txt" or die "C:/3lines.txt $!";
thanks