awk - Pre-populating an array from system command output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - Pre-populating an array from system command output
# 1  
Old 07-01-2010
awk - Pre-populating an array from system command output

So, here's a scenario that requires the same logic as what I'm working on: Suppose that you have a directory containing files named after users. For awk's purposes, the filename is a single field-- something parse-friendly, like john_smith. Now, let's say that I'd like to populate an array in this sort of fashion:

filesize[filename] = $5

That is, the element of the array filesize whose index is the filename will contain that file's size. That's very straightforward if that's all I want to do, but naturally I can't just leave well enough alone. What I'd like to do is be able to do is read in a file (say, a .csv) which has a name field that can be used to recall the file size from the array I just populated. Naturally, there would be other operations going on as well; this is just one element of a larger task. So here's the flow I'm picturing:

1) BEGIN: Execute the command 'ls -al', assigning: filesize[$9] = $5 (give or take, depending on your version of ls...)
2) Perform processing, on a piped stream or file given as an argument, in which filesize[filename] can be recalled as needed.

Here's something that doesn't work...

Code:
#!/usr/bin/awk -f

BEGIN   {
        while ((getline system("ls -al"))
        filesize[$9] = $5
        }

        {
        print "The size of the file " $0 " is " filesize[$0]
        }

If we call that "analyze.awk" and run:

echo "john_smith" | ./analyze.awk

I would expect output like, "The size of the file john_smith is 12395". Of course, I'm here for help, so it doesn't work:

Code:
/usr/bin/awk: syntax error at source line 5 source file ./analyze.awk
 context is
                while ((getline system("ls -al")) >>>
 <<<
/usr/bin/awk: illegal statement at source line 5 source file ./analyze.awk

I strongly suspect that I don't yet really get how getline works. Any ideas how to do this? Many thanks in advance.

Last edited by treesloth; 07-01-2010 at 07:51 PM..
# 2  
Old 07-01-2010
Instead of:
Quote:
Originally Posted by treesloth
Code:
        while ((getline system("ls -al"))

... try:
Code:
while ("ls -al" | getline)

Regards,
Alister
# 3  
Old 07-02-2010
Oh, that's excellent. That is to say, it actually works! Many thanks. Now, I'm off to learn a bit more about getline. I see now that the 'system' bit is simply the exit status of the command, not the output of the command itself; not very useful in this context.

Again, thanks for the help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Please help in populating output in table using shell scripting

Below is my code where i tried using table tag to print out put in table but its not working #!/bin/ksh #] && . ./.profile 2>/dev/null if test -f '.profile'; then . ./.profile; fi; #. .profile LOG_DIR=/app/rpx/jobs/scripts/just/logs sendEmail() { pzCType="$1";... (5 Replies)
Discussion started by: ankit.mca.aaidu
5 Replies

2. Shell Programming and Scripting

Populating a BASH array with a list of files including spaces-in-the-name

For the record, I already tried telling mgmt and the users to disallow spaces in filenames for this script, but it isn't happening for a number of ID10T-error-based reasons. I have simple list of 3 files in a directory that are named like this: bash-3.2$ ls -1 file* file1 file1 part2... (2 Replies)
Discussion started by: ckmehta
2 Replies

3. Shell Programming and Scripting

ksh : need to store the output of a awk command to a array

I have awk command : awk -F ' ' '{ print $NF }' log filename And it gives the output as below: 06:00:00 parameters: SDS (2) no no no no doc=4000000000). information: (6 Replies)
Discussion started by: ramprabhum
6 Replies

4. UNIX for Dummies Questions & Answers

Help populating double quotes using awk

Want to populate double quotes for each filed using awk: Input: cat file.txt => "1-23-test_test1-test2" Required output : "1-23-test_test1-test2"|"#GT_properties_xyz" Was trying the below command on solaris 9 machine : awk -F"|" '{print $1"|""#GT_properties_xyz"}' file.txt ... (8 Replies)
Discussion started by: rajachandhok
8 Replies

5. Shell Programming and Scripting

System Output in to an Array or variable

hey guys in only new to scripting as such, but i have a problem. i want to take the output of a search i do in the command line to then be in a variable but only a certain part of the output. this this what im doing: -bash-2.05b$ ldapsearch -x '(dn:=dc)' dc|grep dc= # base... (1 Reply)
Discussion started by: jmorey
1 Replies

6. Programming

C system() how to send the output to an array??

Hi, im in need if wisdom here guys... How could I store the output of commands like ping integrally directly into a array??? I'll be using system() to run the commands but how can I send the output to an array??? cuz I need to store it so that I can later copy the array's content to a buffer... (5 Replies)
Discussion started by: Jess83
5 Replies

7. Shell Programming and Scripting

Populating an Array

Guys, I need to iterate populate an array while going over files in directory. Can someone please tell me syntax I tried this but it isn't working ==> for F in `ls -p "${directory1}" | grep -v "\/"` do cd "${directory2}" cmp "${directory2}"/"${F}" "${directory1}"/"${F}" ... (2 Replies)
Discussion started by: Veenak15
2 Replies

8. Shell Programming and Scripting

Populating array raised an error

Hi, The following test case populate an array named: array3. Since array1 and array2 are equal in length and values array3 will remain empty. #!/usr/bin/ksh test() { set -A array1 "A" set -A array2 "A" NUM_1=`echo ${#array1}` print "num elenemt in NUM_1 is ${NUM_1}" i=1 for ELE2 in... (1 Reply)
Discussion started by: yoavbe
1 Replies

9. Shell Programming and Scripting

populating array using awk

Hi. I have a file with the following structer: DB DISK LOCATION SIZE ============================================ PROD DATA_01 /dev/dm-23 10 PROD DATA_02 /dev/dm-24 10 PROD DATA_03 /dev/dm-25 10 DEV DATA_04 /dev/dm-26 10 DEV DATA_05 ... (1 Reply)
Discussion started by: yoavbe
1 Replies

10. Shell Programming and Scripting

Awk help with populating variable

Hi, I'm looking for help trying to parse a data stream. Any help would be greatly appreciated. My awk statement is awk '/Aug/{a=$2}/vol/{print a, host, $1, $2, $3, $4, $5}' out.txt Sample Data Stream "out.txt" ----------------------------- # Aug 3 00:00:00 2008 ===== DF =====... (3 Replies)
Discussion started by: jmd2004
3 Replies
Login or Register to Ask a Question