1827293 Members
3535 Online
109717 Solutions
New Discussion

Re: Perl

 
SOLVED
Go to solution
rgoud
Occasional Advisor

Perl

Hi list,
How do I get the similar result using Perl.
APLF=`ls -l /tmp/file.lck |awk '{print $5}' |cut -d: -f1`
I am trying to get size of a file.
Thanks in advance.
7 REPLIES 7
Paddy_1
Valued Contributor

Re: Perl

use the "lstat" command to get the required field
The sufficiency of my merit is to know that my merit is NOT sufficient
John Poff
Honored Contributor

Re: Perl

Hi,

Here is one way to do it in Perl:

#!/usr/bin/perl -w

use File::stat;

$inode = stat("/tmp/file.lck");
$size = $inode->size;
print "Size of /tmp/file.lck is : $size\n";


JP


Paddy_1
Valued Contributor

Re: Perl

use File::Find;
open F,"< /tmp/file.lck" or die "Can't open /tmp/file.lck $!";
my $size = (lstat(F))[7];
print "The Size is : $sizei\n";

The sufficiency of my merit is to know that my merit is NOT sufficient
Robin Wakefield
Honored Contributor

Re: Perl

Hi,

perl -e 'print -s shift,"\n"' /tmp/file.lck

rgds, Robin
S.K. Chan
Honored Contributor
Solution

Re: Perl

or
APLF=`ls -l /tmp/file.lck|perl -e 'while (<>) {print +(split)[4],"\n"}'`
H.Merijn Brand (procura
Honored Contributor

Re: Perl

John, using the OO interface to stat (File::Stat) is maybe easy to read, but a big luggish to use in perl one liners.

Paddy, why use File::Find if you don't use it?

Robin, your post is the best and very correct (as it usually is :), but it can be shorter using the -l command line option, which is very underused:

# perl -le 'print -s shift' /tmp/file.lck

Or even shorter for more files

a5:/u/usr/merijn 103 > perl -le'print-s,$_ for@ARGV' /tmp/xx.txt /tmp/xx.uit
29502/tmp/xx.txt
1230/tmp/xx.uit
a5:/u/usr/merijn 104 >

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Robin Wakefield
Honored Contributor

Re: Perl

Hi Merijn,

You're right of course, always forgot about that one, although if you're assigning to a variable, you don't need the return anyway :)

Robin