Operating System - HP-UX
1833747 Members
2923 Online
110063 Solutions
New Discussion

Perl-script for cutting part of file

 
SOLVED
Go to solution
svilen888
Occasional Advisor

Perl-script for cutting part of file

Could anybody help me with writting of
a procedure in Perl for cutting a part of file "File" and putting this part in another file "File2". This part need to be from certain line /for example: 546/ to the
end of file "File".
Please dont show me examples like
"system (tail -...)".
I'm looking for clearly perl-script.
liana
4 REPLIES 4
H.Merijn Brand (procura
Honored Contributor

Re: Perl-script for cutting part of file

# perl -ne'546..inf and print' file >file2

Simple enough?

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Nicolas Dumeige
Esteemed Contributor

Re: Perl-script for cutting part of file

You can use sed to do that :

sed -n '546,$'p FILE > FILE2

All different, all Unix
Mark Grant
Honored Contributor
Solution

Re: Perl-script for cutting part of file


Apologies to procura for this!

#!/usr/bin/perl

$startline=$ARGV[0];

open FILE1, "open FILE2, ">file2" or die "Couldn't open file2\";

select FILE2;$|=1;
while(){
if(($startline--)<0){
print
}
}
Never preceed any demonstration with anything more predictive than "watch this"
H.Merijn Brand (procura
Honored Contributor

Re: Perl-script for cutting part of file

And if you want to actually truncate file1 at that position, this is an exceptional evil, but short and efficient way to do so

# perl -pi -e'BEGIN{open X,">file2"}546..inf or next;print X;$_="";' file

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn