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...
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:
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..
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)