using unix utility in awk expression 
Author Message
 using unix utility in awk expression

I am wanting to list a directory and output a 2 column list
of files in the directory and each files check sum
(via the unix utility cksum).

That is something like

ls -l DIRECTORY | awk '{printf ("%s \t %s\n", $9 , [cksum $9])}'

The problem as you see is my syntax is not correct. Maybe not even close.
Just a hint in the right direction would be so helpful.
The general question is if it is possible to run a (unix) utlity within
such an awk expression to run against an output?

Thanks,

Mike



Tue, 19 Apr 2005 02:36:45 GMT  
 using unix utility in awk expression

Quote:
> I am wanting to list a directory and output a 2 column list
> of files in the directory and each files check sum
> (via the unix utility cksum).

> That is something like

> ls -l DIRECTORY | awk '{printf ("%s \t %s\n", $9 , [cksum $9])}'

Based on your example, I'm hard-pressed to understand why you're using
awk at all. cksum itself accepts wildcards, so must a simple:

    $ cksum *

reports both the checksum & the file name of each item in the current
directory. Or even if you need first the file name then the checksum
(as in your example), pipe cksum's output to awk instead:

    $  cksum * | awk '
    > {printf "%20.20s\t%s\n",$NF,substr($0,1,match($0,$NF)-1)}'

--
\|/  ______  \|/   If at first you don't succeed, redefine success.

/__| \____/ |__\  
GROG\___U__/       (email: grog-at-sixbit-dot-org)



Tue, 19 Apr 2005 03:17:32 GMT  
 using unix utility in awk expression
Hi,

On 31 Oct 2002 10:36:45 -0800

Quote:

> The general question is if it is possible to run a (unix) utlity within
> such an awk expression to run against an output?

> Thanks,

> Mike

you can use the system funkion :
 system(cmd-line)
like this :

ls -l | awk '{chek=sprintf("cksum %s", $9); printf ("%s\n", $9 , system(chek)) } '

ok, it's not elegant like GROG's one, but it shows how to use unix programms in awk.

Iqbal



Tue, 19 Apr 2005 03:31:03 GMT  
 using unix utility in awk expression


Quote:

>> The general question is if it is possible to run a (unix) utlity
>> within such an awk expression to run against an output?

> you can use the system funkion : system(cmd-line) like this :

> ls -l | awk '{chek=sprintf("cksum %s", $9); printf ("%s\n", $9 ,
> system(chek)) } '

> ok, it's not elegant like GROG's one, but it shows how to use unix
> programms in awk.

ah gee, thanks :?>. Actually your solution answers the text of the
actual question, which was howto use a 'unix utility in awk
expression'.

--
GROG! \|||/        If you're going to do something tonight that
thks -(o o)-       you'll be sorry for tomorrow morning, sleep
/--ooO-(_)-Ooo--\  late.          -- Henny Youngman
(email: grog-at-sixbit-dot-org)



Tue, 19 Apr 2005 04:13:46 GMT  
 using unix utility in awk expression

Quote:
> Hi,

> On 31 Oct 2002 10:36:45 -0800

> > The general question is if it is possible to run a (unix) utlity
within
> > such an awk expression to run against an output?

> > Thanks,

> > Mike

> you can use the system funkion :
>  system(cmd-line)
> like this :

> ls -l | awk '{chek=sprintf("cksum %s", $9); printf ("%s\n", $9 ,
system(chek)) } '

> ok, it's not elegant like GROG's one, but it shows how to use unix
programms in awk.

> Iqbal

The problem with using system() is that its output and that of awk can
be mixed up on the screen.  It is easier to use getline to read the
output of the cksum invocation via a pipe:

ls -l | awk '{("cksum "$9) | getline; print $3"\t"$1}'

gawk will even, on suitable systems, allow a two-way pipe between
processes using the |& operator.

[BTW On my systems cksum outputs the checksum, file size and file name,
so the need for ls and awk is even less there.]

HTH
--
Peter S Tillier  peter{dot}tillier<at>btinternet[dot]com
To email me direct please use the above address
This post represents the views of the author and does not
necessarily accurately represent the views of BT



Tue, 19 Apr 2005 17:08:22 GMT  
 using unix utility in awk expression

Quote:
> I am wanting to list a directory and output a 2 column list

Hmm. I snowed in on '2 column list' . . .
Later i realized that you maybe meant cksum ouput without the size.
Whatever, here is my suggestions.

        bash$ ls
        file0  file1  file2  file3  file4  file5  file6  file7  file8  file9

        bash$ cksum * 2>/dev/null  |pr -2t
        4294967295 0 file0                  4294967295 0 file5
        4294967295 0 file1                  4294967295 0 file6
        4294967295 0 file2                  4294967295 0 file7
        4294967295 0 file3                  4294967295 0 file8
        4294967295 0 file4                  4294967295 0 file9
        bash$

Same thing without the size:

        bash$ cksum * 2>/dev/null  |cut -f1,3 -d' ' |pr -2t
        4294967295 file0                    4294967295 file5
        4294967295 file1                    4294967295 file6
        4294967295 file2                    4294967295 file7
        4294967295 file3                    4294967295 file8
        4294967295 file4                    4294967295 file9
        bash$

//Mats



Tue, 19 Apr 2005 18:36:58 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. RS regular expression with Unix AWK

2. AWK: Using variables in regular Expressions

3. AWK: Using variables in regular expressions

4. Using Unix commands in AWK

5. How can I sort a fixed width file using awk on a HP unix systems

6. Outputting info from awk and using the info to name the awk output file

7. Math expressions evaluating utility

8. *awk utility for extracting an uue file

9. Newbie awk (sed??) question, regular expressions

10. newbie needs help with multiple expressions in awk

11. Variable in awk expression

12. awk pattern as a variable with regular expression

 

 
Powered by phpBB® Forum Software