[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

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



0
gauravflame
Asked:
gauravflame
  • 10
  • 9
  • 2
1 Solution
 
ozoCommented:
open NEW,">3lines.txt" or die "3lines.txt $!";
print NEW @raw_data[-3..-1];
close NEW;
0
 
gauravflameAuthor Commented:
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;

0
 
ozoCommented:
$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];
close NEW;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gauravflameAuthor Commented:
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;
0
 
ozoCommented:
That means that there were less than 3 lines in @raw_data
0
 
gauravflameAuthor Commented:
right now  code is working fine but not able to create 3lines.txt file.

10lines.txt file has 10 lines , I make sure it
0
 
ozoCommented:
What do you see from
print @raw_data, scalar @raw_data;
0
 
gauravflameAuthor Commented:
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
0
 
ozoCommented:
Can you post the entire program and any error messages you get
0
 
gauravflameAuthor Commented:
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 ";
0
 
ghostdog74Commented:
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";
0
 
ozoCommented:
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 $!;
0
 
gauravflameAuthor Commented:
things become complex with last 20 lines
0
 
ozoCommented:
Do you get any errors from
print NEW @raw_data[-3..-1] or die $!;
close NEW or die $!;
0
 
gauravflameAuthor Commented:
ozo,
I am not getting any error messages not even warning  , but not able to create new file 3lines.txt
0
 
gauravflameAuthor Commented:
I used the die not dire!
0
 
ozoCommented:
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)
0
 
gauravflameAuthor Commented:
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
0
 
ghostdog74Commented:
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...
0
 
ozoCommented:
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 $!";
0
 
gauravflameAuthor Commented:
I got it with

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

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 10
  • 9
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now