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

R

robertchen117

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.
 
R

robertchen117

`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.
 
J

John W. Krahn

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
 
M

Mumia W.

`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.
 
P

Peter J. Holzer

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
 
J

John W. Krahn

Peter said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top