1830244 Members
2006 Online
109999 Solutions
New Discussion

perl question

 
SOLVED
Go to solution
Ridzuan Zakaria
Frequent Advisor

perl question

Hi,

How do write the below code in perl?

LN="rwxrwxr-x 3 oracle dba 96 Sep 16 16:48 datafile01.dbf";

FLNM=`echo $LN | awk '{print($9)}'`

I want to read the 9th column of the LN string.

Thanks.
ridzuan
quest for perfections
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: perl question

This is one of those things that awk is better at doing but here is one Perl solution:

echo "${LN}" | perl -e 'while (<>) {@a = split ' ',$_; print $a[8],"\n";}'
If it ain't broke, I can fix that.
Hein van den Heuvel
Honored Contributor

Re: perl question

There are of course many ways to solve this:

With an array, after a split:

perl -e '$_ = `ls -l x`; @words=split; print "$words[8]\n"'


Using a regular expression to remember in $1 the last series of non-spaces on the line:

perl -e '$_ = `ls -l x`; if (/\s(\S+)$/) { print "$1\n" }'


Using the same split but labeling the returned array with variables"

perl -e '$_ = `ls -l x`;($prot,$x,$group,$user,$size,$date1,$date1,$time,$name)=split; print "$name\n"'

Cheers,
Hein.



Caesar_3
Esteemed Contributor

Re: perl question

Hello!

In perl script:
$LN = "rwxrwxr-x 3 oracle dba 96 Sep 16 16:48 datafile01.dbf";

$name = (split (" ", $LN))[8];

Caesar
H.Merijn Brand (procura
Honored Contributor

Re: perl question

Don't count :) It's the last field:

$name = (split/\s+/)[-1];

why use external commands to get to file stuff? Because it's from a list? Good reason, otherwise use stat

Enjoy, have Fun! H.Merijn
Enjoy, Have FUN! H.Merijn
A. Clay Stephenson
Acclaimed Contributor

Re: perl question

By the way, if you have a working awk example, you can simply run it through a2p to translate it to Perl. It doesn't always produce pretty Perl but it does produce a workable solution that you can then modify.
If it ain't broke, I can fix that.
Ridzuan Zakaria
Frequent Advisor

Re: perl question

Both solutions work just fine. Thanks.
quest for perfections