Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to put the command ls -l| awk '{print $1" "$5" "$9}' in a perl script?

Reply
Thread Tools

How to put the command ls -l| awk '{print $1" "$5" "$9}' in a perl script?

 
 
robertchen117@gmail.com
Guest
Posts: n/a
 
      04-02-2007
hi all,

In a Unix server,
#cd /tmp
# ls -l| awk '{print $1" "$5" "$9}'
total
-rw------- 159045 1IUPKPnHDb
-rw------- 1287908 1Mbi68lSf3
-rw------- 1475 1o3Bj5Dc1G
....

My perl is:

#!/vendor/perl/bin/perl

#`ls -l| awk '{print $1" "$5" "$9}' > /tmp/ls.out`; #This does not
work!
#system("ls -l| awk '{print $1" "$5" "$9}' > /tmp/ls.out");
#compilation error
system("ls -l| awk '{print $1\" \"$5\" \"$9}' > /tmp/ls.out"); #The
File is generated but with empty lines.

I tried the above 3 commands all failed.
How to put the command ls -l| awk '{print $1" "$5" "$9}' in a perl
script?

Thanks.

 
Reply With Quote
 
 
 
 
robertchen117@gmail.com
Guest
Posts: n/a
 
      04-02-2007
`ls -l| awk '{print $1" "$5" "$9}' > /tmp/ls.out`; #put this in perl,
could not get right result

system("ls -l| awk '{print $1" "$5" "$9}' > /tmp/ls.out"); #put this
in perl, compile error

system("ls -l| awk '{print $1\" \"$5\" \"$9}' > /tmp/ls.out"); ##put
this in perl, also could not get right result

Thanks.
On 4月2日, 上午10时59分, "(E-Mail Removed)" <(E-Mail Removed)>
wrote:
> hi all,
>
> In a Unix server,
> #cd /tmp
> # ls -l| awk '{print $1" "$5" "$9}'
> total
> -rw------- 159045 1IUPKPnHDb
> -rw------- 1287908 1Mbi68lSf3
> -rw------- 1475 1o3Bj5Dc1G
> ...
>
> My perl is:
>
> #!/vendor/perl/bin/perl
>
> #`ls -l| awk '{print $1" "$5" "$9}' > /tmp/ls.out`; #This does not
> work!
> #system("ls -l| awk '{print $1" "$5" "$9}' > /tmp/ls.out");
> #compilation error
> system("ls -l| awk '{print $1\" \"$5\" \"$9}' > /tmp/ls.out"); #The
> File is generated but with empty lines.
>
> I tried the above 3 commands all failed.
> How to put the command ls -l| awk '{print $1" "$5" "$9}' in a perl
> script?
>
> Thanks.



 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      04-02-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> In a Unix server,
> #cd /tmp
> # ls -l| awk '{print $1" "$5" "$9}'
> total
> -rw------- 159045 1IUPKPnHDb
> -rw------- 1287908 1Mbi68lSf3
> -rw------- 1475 1o3Bj5Dc1G
> ...
>
> My perl is:
>
> #!/vendor/perl/bin/perl
>
> #`ls -l| awk '{print $1" "$5" "$9}' > /tmp/ls.out`; #This does not
> work!
> #system("ls -l| awk '{print $1" "$5" "$9}' > /tmp/ls.out");
> #compilation error
> system("ls -l| awk '{print $1\" \"$5\" \"$9}' > /tmp/ls.out"); #The
> File is generated but with empty lines.
>
> I tried the above 3 commands all failed.
> How to put the command ls -l| awk '{print $1" "$5" "$9}' in a perl
> script?


use Fcntl ':mode';

for my $file ( <*> ) {
my ( $mode, $size ) = ( stat $file )[ 2, 7 ];
print +
($mode & S_IFDIR) ? 'd' : '-',
($mode & S_IRUSR) ? 'r' : '-',
($mode & S_IWUSR) ? 'w' : '-',
($mode & S_IXUSR) ? 'x' : '-',
($mode & S_IRGRP) ? 'r' : '-',
($mode & S_IWGRP) ? 'w' : '-',
($mode & S_IXGRP) ? 'x' : '-',
($mode & S_IROTH) ? 'r' : '-',
($mode & S_IWOTH) ? 'w' : '-',
($mode & S_IXOTH) ? 'x' : '-',
" $size $file";
}




John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
 
Reply With Quote
 
Mumia W.
Guest
Posts: n/a
 
      04-02-2007
On 04/01/2007 10:06 PM, (E-Mail Removed) wrote:
> `ls -l| awk '{print $1" "$5" "$9}' > /tmp/ls.out`; #put this in perl,
> could not get right result
>
> system("ls -l| awk '{print $1" "$5" "$9}' > /tmp/ls.out"); #put this
> in perl, compile error
>
> system("ls -l| awk '{print $1\" \"$5\" \"$9}' > /tmp/ls.out"); ##put
> this in perl, also could not get right result
> [...]


use File::Slurp;
my $data = `ls -l | awk '{print \$1, \$5, \$8}'`;
write_file('/tmp/ls.out', $data);

Mostly you just needed backslashes before the dollar signs.
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      04-02-2007
On 2007-04-02 04:56, John W. Krahn <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
>> In a Unix server,
>> #cd /tmp
>> # ls -l| awk '{print $1" "$5" "$9}'
>> total
>> -rw------- 159045 1IUPKPnHDb
>> -rw------- 1287908 1Mbi68lSf3
>> -rw------- 1475 1o3Bj5Dc1G
>> ...

[...]
>> How to put the command ls -l| awk '{print $1" "$5" "$9}' in a perl
>> script?

>
> use Fcntl ':mode';
>
> for my $file ( <*> ) {
> my ( $mode, $size ) = ( stat $file )[ 2, 7 ];
> print +
> ($mode & S_IFDIR) ? 'd' : '-',

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Unix knows more file types than directories and regular files.

hp


--
_ | Peter J. Holzer | Blaming Perl for the inability of programmers
|_|_) | Sysadmin WSR | to write clearly is like blaming English for
| | | (E-Mail Removed) | the circumlocutions of bureaucrats.
__/ | http://www.hjp.at/ | -- Charlton Wilbur in clpm
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      04-02-2007
Peter J. Holzer wrote:
> On 2007-04-02 04:56, John W. Krahn <(E-Mail Removed)> wrote:
>>(E-Mail Removed) wrote:
>>>In a Unix server,
>>>#cd /tmp
>>># ls -l| awk '{print $1" "$5" "$9}'
>>>total
>>>-rw------- 159045 1IUPKPnHDb
>>>-rw------- 1287908 1Mbi68lSf3
>>>-rw------- 1475 1o3Bj5Dc1G
>>>...

> [...]
>>>How to put the command ls -l| awk '{print $1" "$5" "$9}' in a perl
>>>script?

>>use Fcntl ':mode';
>>
>>for my $file ( <*> ) {
>> my ( $mode, $size ) = ( stat $file )[ 2, 7 ];
>> print +
>> ($mode & S_IFDIR) ? 'd' : '-',

> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> Unix knows more file types than directories and regular files.


I know Peter. I just wanted to keep it simple for this example.



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
awk's NF in Perl Thorsten Gottschalk Perl 1 03-05-2004 08:04 PM
using perl on the command line, like sed or awk gorda Perl Misc 3 10-21-2003 01:50 PM
using perl on the command line, like sed or awk gorda Perl 2 10-21-2003 06:38 AM
sed awk or perl for this? NNTP Perl 13 09-30-2003 06:49 AM



Advertisments