CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Have to exclude the first and last line of the file : help me

 



vishwakar
Novice

Dec 22, 2010, 8:39 AM

Post #1 of 24 (4032 views)
Have to exclude the first and last line of the file : help me Can't Post

i ll have a file which look likes



Code
123 |something |567 
456 |welcome |789
457 |inboxpost |790
.
.
123 |something |567



i have to execute all the lines in the file except the first and the last.///
can u gice me idea and how to proceed with this...


Zhris
Enthusiast

Dec 22, 2010, 5:20 PM

Post #2 of 24 (4025 views)
Re: [vishwakar] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

You could read the file into an array if it isn't too large, then "shift" the first element and "pop" the last:


Code
open my $inputfh, '<', 'Input.txt' or die "cannot open Input.txt - $!"; 
my @lines = <$inputfh>;
close $inputfh;

shift @lines;
pop @lines;


Chris


vishwakar
Novice

Dec 22, 2010, 7:26 PM

Post #3 of 24 (4020 views)
Re: [Zhris] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

ya thank u for replying .. but here my problem is first i ll take all the files in to an array after sorting them i ll use the first element in the array as the input file to be opened . can i again take them in to an array ?

Code
 
open my $inputfh, '<', '$list[0]' ;
my @lines = <$inputfh>;
close $inputfh;
shift @lines;
pop @lines;



vishwakar
Novice

Dec 23, 2010, 8:39 AM

Post #4 of 24 (4006 views)
Re: [vishwakar] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

i tried the logic u said .. but its not printing anything... but there is no scriptting error.. its also not doing what i required


FishMonger
Veteran / Moderator

Dec 23, 2010, 9:23 AM

Post #5 of 24 (4002 views)
Re: [vishwakar] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

Change:

Code
open my $inputfh, '<', '$list[0]' ;


To:

Code
open my $inputfh, '<', $list[0] or die "failed to open <$list[0]> $!";



vishwakar
Novice

Dec 24, 2010, 11:09 AM

Post #6 of 24 (3991 views)
Re: [FishMonger] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

ya i made above change and run the code... but it does not remove the first and last line in my file


Zhris
Enthusiast

Dec 24, 2010, 11:26 AM

Post #7 of 24 (3988 views)
Re: [vishwakar] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

The array "@lines" contains all the lines in your input file except the first and the last. You need to then output the array to your output file.

If you want to edit a file in place, you would need to look into a module like Tie::File. This module is also great for handling large data.


Code
open my $inputfh, '<', 'Input.txt' or die "cannot open Input.txt - $!"; 
my @lines = <$inputfh>;
close $inputfh;

shift @lines;
pop @lines;

open my $outputfh, '>', 'Output.txt' or die "cannot open Output.txt - $!";
print $outputfh "@lines";
close $outputfh;

#rename ('Output.txt', 'Input.txt');


Chris


(This post was edited by Zhris on Dec 24, 2010, 11:35 AM)


vishwakar
Novice

Dec 24, 2010, 11:37 AM

Post #8 of 24 (3978 views)
Re: [Zhris] Have to exclude the first and last line of the file : help me [In reply to] Can't Post



opendir(DIR, $DATDIR) or die "can't opendir $dir: $! \n";
@files=readdir(DIR);
closedir DIR;
foreach $file (@files) {
if (-f "$dir$file")
{
#$moveloc="/export/elk2/sm425k/new/"; # Make sure the directory is already made.
#$new = "$moveloc";
$old = "$dir$file";
$new = "$WORKDIR";
move($old, $new) or die "Copy Faild: $!";
}
}

@list = reverse sort(@files);

open(DAT, "$WORKDIR/$list[0]") || die("Could not open file!");
open my $inputfh, '<', '$list[0]' ;
my @lines = <$inputfh>;
close $inputfh;
shift @lines;
pop @lines;
foreach $line(@lines)
{
print " $line \n";
}


Code
 

ya i know that the array consists of the data
which has removed first and last line.. but the
above logic i am using is not doing that ..

first i ll get a number of files ,, i have to move them
to a directory and select a file with the latest time stamp..
so i used reverse sort which was working fine
so $list[0] consits of the file which i have to read
and will be as mentioned above...
i think i made u clear.. please can we use some what like
nested array in perl .. i meant array inside a
array inside array


(This post was edited by vishwakar on Dec 24, 2010, 11:46 AM)


Zhris
Enthusiast

Dec 24, 2010, 11:48 AM

Post #9 of 24 (3974 views)
Re: [vishwakar] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

I'm really sorry, i'm still unclear exactly what you are trying to achieve. What is the nested array for, what data is it meant to store. If its possible to do so, provide example output. But sorry, at this time, i'm a little lost as to what you require to know.

Whats this line for, you don't seem to use the filehandle at all and theres no read/write/append operator:

Code
open(DAT, "$WORKDIR/$list[0]") || die("Could not open file!");


Chris


(This post was edited by Zhris on Dec 24, 2010, 11:54 AM)


vishwakar
Novice

Dec 24, 2010, 12:01 PM

Post #10 of 24 (3963 views)
Re: [Zhris] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

there will be some hundred number of files in which i
have to slect latest thing with time stamp..
so i took all of the files in to an array and reverse sorted
them and got the file with latest stamp..
that is the file $list[0] which look like this
this is the file which has data inside it and has to be processed

Code
HEADER|234 
421|ADD|345
422|INSERT|567
421|INSERT|588
445|MULTIPLY|596
TAILER|234

i have to read and execute like this

Code
421|ADD|345 
422|INSERT|567
421|INSERT|588
445|MULTIPLY|596


ya zhris i tried to make u clear..
please correct me if was wrong any where


(This post was edited by vishwakar on Dec 24, 2010, 12:09 PM)


Zhris
Enthusiast

Dec 24, 2010, 2:52 PM

Post #11 of 24 (3958 views)
Re: [vishwakar] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

Ok I am clearer as i've taken more time to look at your code and task. However, you haven't really explained anything further from your original post, and I don't understand what is it your code isn 't doing. I don't think theres anything wrong with the code provided to you proceeding your original question. I reckon it could be a path issue specifically when moving files or there are errors in your syntax.

Can you explain what this section is meant to do:

Code
foreach $file (@files) {  
if (-f "$dir$file")
{
#$moveloc="/export/elk2/sm425k/new/"; # Make sure the directory is already made.
#$new = "$moveloc";
$old = "$dir$file";
$new = "$WORKDIR";
move($old, $new) or die "Copy Faild: $!";
}
}


From first glance, it looks as if you are moving any "files" you want to deal with (all of them), to another directory, for no particular reason. For starters, is $WORKDIR a constant, if so then you'll end up with 1 file because it will already exist after the first loop. I think you meant to do "$WORKDIR/$file" but i'm not 100% sure what you are attempting to achieve in this block of code. Another thing, ensure that your code/namespacing is consistant. I can see that you do "$dir$file" sometimes and "$dir/$file" other times. I would stick to using the latter throughout.

Well, I can't test any code today because my web server is down and i'm not on a machine with perl installed, but here is my version of your code from how I understand what you are trying to do. Don't know if it will work properly (untested), or if its helpful to you at all, but I hope its along the right lines. If you are certain the code is error-free, explain exactly what it is the code is not doing that you want it to do, or is doing that you don't want it to do:


Code
#! /usr/bin/perl 
use strict;
use warnings;
use File::Copy;

#Paths
my $datdir = 'Dir/To/Data/';
my $workdir = 'Dir/To/New/Data/Dir/';

#Retrieve and move files
my @files = grep { (-f "$datdir/$_") } glob ("$datdir/*");
foreach my $file (@files) {
my $oldloc = "$datdir/$file";
my $newloc = "$workdir/$file";
move ($oldloc, $newloc) or die "Failed to move $oldloc to $newloc: $!";
}

#Get latest file (reversed lexographical sort).
my ($latestfile, @otherfiles) = reverse sort (@files);

#Get lines
open my $inputfh, '<', "$workdir/$latestfile" or die "Failed to open $workdir/$latestfile: $!";
my @lines = <$inputfh>;
close $inputfh;

#Remove first and last lines
shift @lines;
pop @lines;

#Print lines
foreach my $line (@lines) {
my @cells = split /\|/, $line;
print "@cells";
}


Merry christmas,

Chris


(This post was edited by Zhris on Dec 24, 2010, 4:05 PM)


vishwakar
Novice

Dec 24, 2010, 9:11 PM

Post #12 of 24 (3930 views)
Re: [Zhris] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

ya i ll make u clear.

Code
in the below code i am moving all the files in a directory. 
foreach $file (@files) {
if (-f "$dir$file")
{
#$moveloc="/export/elk2/sm425k/new/"; # Make sure the directory is already made.
#$new = "$moveloc";
$old = "$dir$file";
$new = "$WORKDIR";
move($old, $new) or die "Copy Faild: $!";
}
}
input file = data directory
i should process it in work directory
actually i will get the input file in a data directory. so whenever i run the script i want to move all the files in that directory to the work directory and pick the latest file and process it.


Zhris
Enthusiast

Dec 25, 2010, 4:54 AM

Post #13 of 24 (3916 views)
Re: [vishwakar] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

Did you test my last snippet of code and see if it works for you.

Chris


vishwakar
Novice

Dec 25, 2010, 8:58 AM

Post #14 of 24 (3898 views)
Re: [Zhris] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

ya zhris.. i ll test the code and tell u ..


vishwakar
Novice

Dec 26, 2010, 10:18 PM

Post #15 of 24 (3871 views)
Re: [Zhris] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

while i am runnin gur code i am facing with this problem,,

perl zhris.pl
Use of uninitialized value in concatenation (.) or string at zhris.pl line 24.
this was line 24..
open my $inputfh, '<',"$workdir/$latestfile" or die "Failed to open $workdir/$latestfile $!";


vishwakar
Novice

Dec 26, 2010, 10:37 PM

Post #16 of 24 (3870 views)
Re: [vishwakar] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

this was ur logic i was using .. to get the output..

Code
#!/usr/bin/perl 
use File::Copy;
use IO::File;
open my $inputfh, '<',"latestfile" or die "Failed to open latestfile $!";
my @lines = <$inputfh>;
close $inputfh;

#Remove first and last lines
my @test =shift @lines;
my @last =pop @test;


#Print lines
foreach my $last (@last) {
my @cells = split /\|/, $last;
print "@cells";
}

this was the output

$ perl text.pl
789 ADD 10
790 ADD 20
791 CHANGE 30
791 DISCONNECT 10
792 ADD 15
TRL 000000005


it was not removing the last line


FishMonger
Veteran / Moderator

Dec 28, 2010, 6:13 AM

Post #17 of 24 (3843 views)
Re: [vishwakar] Have to exclude the first and last line of the file : help me [In reply to] Can't Post


In Reply To
this was ur logic i was using .. to get the output..

Code
#!/usr/bin/perl 
use File::Copy;
use IO::File;
open my $inputfh, '<',"latestfile" or die "Failed to open latestfile $!";
my @lines = <$inputfh>;
close $inputfh;

#Remove first and last lines
my @test =shift @lines;
my @last =pop @test;


#Print lines
foreach my $last (@last) {
my @cells = split /\|/, $last;
print "@cells";
}

this was the output

$ perl text.pl
789 ADD 10
790 ADD 20
791 CHANGE 30
791 DISCONNECT 10
792 ADD 15
TRL 000000005


it was not removing the last line


That script won't produce the output you claim, unless you change this:

Code
foreach my $last (@last) {

to this:

Code
foreach my $last (@lines) {


Never retype test code in a post. Always copy/paste your actual code.

Here's an example script showing the approach I'd take.

Code
#!/usr/bin/perl 

use strict;
use warnings;

<DATA>; # throw away the first line

while ( <DATA> ) {
last if eof; # throw away the last line
tr /|/ /;
print;
}


__DATA__
HEADER|234
421|ADD|345
422|INSERT|567
421|INSERT|588
445|MULTIPLY|596
TAILER|234


Here's the results:

Quote
E:\TEMP\test>test.pl
421 ADD 345
422 INSERT 567
421 INSERT 588
445 MULTIPLY 596



(This post was edited by FishMonger on Dec 28, 2010, 6:14 AM)


Zhris
Enthusiast

Dec 28, 2010, 1:39 PM

Post #18 of 24 (3833 views)
Re: [FishMonger] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

Thats a nice way of removing the first and last lines. I didn`t know it could be done like that.

Chris


FishMonger
Veteran / Moderator

Dec 28, 2010, 6:28 PM

Post #19 of 24 (3831 views)
Re: [Zhris] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

I try to avoid slurping a file into an array because it increases the memory footprint and Tie::File, while being convenient, it's fairly slow.

However, if you have large files where you can't iterate it line-by-line, then Tie::File is usually the best approach.


vishwakar
Novice

Dec 28, 2010, 11:58 PM

Post #20 of 24 (3825 views)
Re: [FishMonger] Have to exclude the first and last line of the file : help me [In reply to] Can't Post


Code
$ cat text.pl 
#!/usr/bin/perl
use File::Copy;
use IO::File;
open my $inputfh, '<',"latestfile" or die "Failed to open latestfile $!";
my @lines = <$inputfh>;
close $inputfh;

#Remove first and last lines
my @test =shift @lines;
my @last =pop @test;


#Print lines
foreach my $last (@lines) {
my @cells = split /\|/, $last;
print "@cells";
} $ perl text.pl
789 ADD 10
790 ADD 20
791 CHANGE 30
791 DISCONNECT 10
792 ADD 15
TRL 000000005



i hade made the change u asked me to do.. but there is no change in out put .please can u help me out .


FishMonger
Veteran / Moderator

Dec 29, 2010, 5:26 AM

Post #21 of 24 (3823 views)
Re: [vishwakar] Have to exclude the first and last line of the file : help me [In reply to] Can't Post


Quote
i hade made the change u asked me to do.


No you didn't, which is why your script is not doing what you want.


vishwakar
Novice

Dec 29, 2010, 5:50 AM

Post #22 of 24 (3820 views)
Re: [FishMonger] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

i am really sorry, i had made the changes what i understood can u please correct the code and paste.


FishMonger
Veteran / Moderator

Dec 29, 2010, 6:26 AM

Post #23 of 24 (3817 views)
Re: [vishwakar] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

If you want to use the approach that Chris showed, then do this:

Code
#!/usr/bin/perl 

use strict;
use warnings;
use File::Copy;
use IO::File;

open my $inputfh, '<',"latestfile" or die "Failed to open latestfile $!";

my @lines = <$inputfh>;
close $inputfh;

#Remove first and last lines
shift @lines;
pop @lines;


#Print lines
foreach my $last (@lines) {
my @cells = split /\|/, $last;
print "@cells";
}


If you want to use the approach I showed, then do this:

Code
#!/usr/bin/perl 

use strict;
use warnings;
use File::Copy;
use IO::File;

open my $inputfh, '<',"latestfile" or die "Failed to open latestfile $!";

<$inputfh>; # throw away the first line

while ( my $line = <$inputfh> ) {
last if eof; # throw away the last line
$line =~ tr /|/ /;
print $line;
}

close $inputfh;



vishwakar
Novice

Dec 29, 2010, 6:38 AM

Post #24 of 24 (3815 views)
Re: [FishMonger] Have to exclude the first and last line of the file : help me [In reply to] Can't Post

thank u , i ll test the code tomorrow.. and post if its works fine.. thank u a lot in making me clear

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives