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

Discussion in 'Perl Misc' started by robertchen117@gmail.com, Apr 2, 2007.

  1. Guest

    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.
     
    , Apr 2, 2007
    #1
    1. Advertising

  2. Guest

    `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·Ö, "" <>
    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.
     
    , Apr 2, 2007
    #2
    1. Advertising

  3. Re: How to put the command ls -l| awk '{print $1" "$5" "$9}' in aperl script?

    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
     
    John W. Krahn, Apr 2, 2007
    #3
  4. Mumia W. Guest

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

    On 04/01/2007 10:06 PM, 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.
     
    Mumia W., Apr 2, 2007
    #4
  5. On 2007-04-02 04:56, John W. Krahn <> wrote:
    > 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
    | | | | the circumlocutions of bureaucrats.
    __/ | http://www.hjp.at/ | -- Charlton Wilbur in clpm
     
    Peter J. Holzer, Apr 2, 2007
    #5
  6. Re: How to put the command ls -l| awk '{print $1" "$5" "$9}' in aperl script?

    Peter J. Holzer wrote:
    > On 2007-04-02 04:56, John W. Krahn <> wrote:
    >> 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
     
    John W. Krahn, Apr 2, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gorda
    Replies:
    2
    Views:
    553
    Andrew Shitov
    Oct 21, 2003
  2. engineer

    Tool for perl script to awk

    engineer, Jun 14, 2004, in forum: Perl
    Replies:
    1
    Views:
    917
    Joe Smith
    Jun 14, 2004
  3. gorda
    Replies:
    3
    Views:
    159
    Barry Kimelman
    Oct 21, 2003
  4. Perl script to replace awk

    , Jul 26, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    148
    Geoffroy Braem
    Jul 27, 2004
  5. AyOut

    Executing awk from perl script

    AyOut, Mar 24, 2007, in forum: Perl Misc
    Replies:
    6
    Views:
    165
    Greg Bacon
    Mar 26, 2007
Loading...

Share This Page