Execute command from awk

This seemed straight forward until I tried to code it.

I'm running ioscan -fuNC disk to get list of disk including hardware path. What I'm trying to do is use awk to take this output, extract hardware path, and pass that as a variable to

ioscan -P wwid -N -H 64000/0xfa00/0x1a | grep ^disk

and then add the wwid to the rest of the original commands output. Below is basically psuedo code of what I'm trying to get at.

ioscan -P wwid -N -C disk | awk 'var1=(ioscan -P wwid -N -H $3 | grep ^disk | awk '{print $4}') {print $1 $2 $3 $4 $var1}'

Question is how do I run the ioscan from within awk and pass in the field variable as input and assign the return to a variable?

or

If someone knows how to run ioscan to get all this in one shot that would be great also.

Thanks.

Please do not leave people guessing. Show a representative sample of input, desired output, attempts at a solution and specify what OS and versions being used, or this thread will be closed.

You can do that 'outside' of awk, incidentally, with awk -v VAR="`command`".

But if you show the input you have and output you want we can help you find better ways.

Sorry,
OS is hpux 11.31.

Expected input.

root:common dc40rx:/home/oracle # ioscan -fuNC disk
Class     I  H/W Path  Driver S/W State   H/W Type     Description
===================================================================
disk      3  64000/0xfa00/0x0   esdisk   CLAIMED     DEVICE       HP      DG072A9BB7
disk      4  64000/0xfa00/0x1   esdisk   CLAIMED     DEVICE       HP      DG072A8B54
disk      5  64000/0xfa00/0x2   esdisk   CLAIMED     DEVICE       HP      DG072A8B54
disk      7  64000/0xfa00/0x3   esdisk   CLAIMED     DEVICE       TEAC    DVD-ROM DW-224EV
disk     19  64000/0xfa00/0x9   esdisk   CLAIMED     DEVICE       EMC     SYMMETRIX
disk     20  64000/0xfa00/0xa   esdisk   CLAIMED     DEVICE       EMC     SYMMETRIX
disk     21  64000/0xfa00/0xb   esdisk   CLAIMED     DEVICE       EMC     SYMMETRIX
disk     22  64000/0xfa00/0xc   esdisk   CLAIMED     DEVICE       EMC     SYMMETRIX
disk     25  64000/0xfa00/0xd   esdisk   CLAIMED     DEVICE       EMC     SYMMETRIX
disk     34  64000/0xfa00/0x10  esdisk   CLAIMED     DEVICE       DGC     CX500WDR5
disk     35  64000/0xfa00/0x11  esdisk   CLAIMED     DEVICE       DGC     CX500WDR5
disk     41  64000/0xfa00/0x12  esdisk   CLAIMED     DEVICE       EMC     SYMMETRIX
disk     42  64000/0xfa00/0x13  esdisk   CLAIMED     DEVICE       EMC     SYMMETRIX
disk     47  64000/0xfa00/0x19  esdisk   CLAIMED     DEVICE       DGC     CX500WDR5
disk     48  64000/0xfa00/0x1a  esdisk   CLAIMED     DEVICE       DGC     CX500WDR5
disk     49  64000/0xfa00/0x1b  esdisk   CLAIMED     DEVICE       DGC     CX500WDR5
disk     50  64000/0xfa00/0x1c  esdisk   CLAIMED     DEVICE       DGC     CX500WDR5

Expected output is same with wwid as the 9th column.

Hmm.

awk -v lastcol="LASTCOL" 'NR>2 { $(NF+1)=lastcol } 1'

You can put whatever you want for LASTCOL, including things in backticks, since it's not inside the awk code itself, it's just another string in a commandline.

I'm following what you're doing but I think I still need the field data from the first command to act as input to the second while still retaining the other field values for printing the output. I think I'm just going to append both outputs to a file, load in database, and reaggregate using SQL. thanks.

You mean, the value of lastcol needs to change while it's running?

I think he means something like:

ioscan | awk '
{
        cmd="ioscan $3";
        while ((cmd | getline l) > 0)
                if (l ~ /^disk/) wwid=$4
        close(cmd)
        print $0, wwid
}'

totally untested

What does the output of

ioscan -P wwid -N -H 64000/0xfa00/0x1a

look like?

Looks like this.

root:common dc40rx:/ # ioscan -P wwid -N -H 64000/0xfa00/0x1a
Class     I  H/W Path  wwid
===============================
disk     48  64000/0xfa00/0x1a  0x600601609ff11200b3fdd7672841e111

---------- Post updated at 11:26 AM ---------- Previous update was at 11:21 AM ----------

Basically I'm trying to get ioscan with all the information from fuNC but to also include the wwid. Someone else mentioned scsimgr which, with the appropiate flags, might do the trick but I just got another requirement to include volume group information as well. Getting more complicated than I thought. I might have to take a different track altogether. Thanks everyone for the input.

It's quite possible to use awk to combine multiple inputs. It may take more than a single pipe but you can certainly do it.

So, ioscan outputs a single wwid which you wish to use as the last column to all rows? Or multiple wwid's which you wish to join to different rows depending on matching columns? We'll need to see context here. You're posting the bare minimum which leaves too many unanswered questions.

I created a fake "ioscan" which displays output like yours. the script is entirely awk. it'd probably be done better in shell, but i'm not certain which that is.

#!/usr/bin/awk -f
BEGIN {
        listcmd = "ioscan -fuNC disk"
        while ((listcmd | getline) > 0) {
                if ($1 != "disk")
                        continue
                wwidcmd="ioscan -P wwid -N -H " $3
                while ((wwidcmd | getline wwidline) > 0) {
                        if (wwidline !~ /^disk/)
                                continue
                        n=split(wwidline, a, /[[:space:]]*/)
                        if (a[3] == $3) print $0,a[4]
                }
                close(wwidcmd)
        }
}

produces output like this:

[mute@geek ~]$ ./script
disk      3  64000/0xfa00/0x0   esdisk   CLAIMED     DEVICE       HP      DG072A9BB7 0xd9ef341e08df0449ab6e28a6698b8ed1
disk      4  64000/0xfa00/0x1   esdisk   CLAIMED     DEVICE       HP      DG072A8B54 0xa3a7d748a36dfe2838c22cf71e79b25f
disk      5  64000/0xfa00/0x2   esdisk   CLAIMED     DEVICE       HP      DG072A8B54 0xeb955194471d1b877293b54ba7c12807
disk      7  64000/0xfa00/0x3   esdisk   CLAIMED     DEVICE       TEAC    DVD-ROM DW-224EV 0x17ed66ede9042c52d6570e66ea8c4112
disk     19  64000/0xfa00/0x9   esdisk   CLAIMED     DEVICE       EMC     SYMMETRIX 0x0e90354e6c63d4b66a9fe1e8191cd0d5
disk     20  64000/0xfa00/0xa   esdisk   CLAIMED     DEVICE       EMC     SYMMETRIX 0x35c07d3d928df2c0196e46a6296c8768
disk     21  64000/0xfa00/0xb   esdisk   CLAIMED     DEVICE       EMC     SYMMETRIX 0x2b6c01e7a6591d730234fd03c909d943
disk     22  64000/0xfa00/0xc   esdisk   CLAIMED     DEVICE       EMC     SYMMETRIX 0xb403a1eb43fe26e4c5eadfc366554f29
disk     25  64000/0xfa00/0xd   esdisk   CLAIMED     DEVICE       EMC     SYMMETRIX 0x0b4a30a946bdcc35812f341d52577148
disk     34  64000/0xfa00/0x10  esdisk   CLAIMED     DEVICE       DGC     CX500WDR5 0x83c9c360f1dd2b200c26d997bc5d19b6
disk     35  64000/0xfa00/0x11  esdisk   CLAIMED     DEVICE       DGC     CX500WDR5 0xa33398f2469a04d6e6666696c9494569
disk     41  64000/0xfa00/0x12  esdisk   CLAIMED     DEVICE       EMC     SYMMETRIX 0x71db7f9609f641977acc1c3efa74fa19
disk     42  64000/0xfa00/0x13  esdisk   CLAIMED     DEVICE       EMC     SYMMETRIX 0x0813fa24f883561fee832cbafd13ae41
disk     47  64000/0xfa00/0x19  esdisk   CLAIMED     DEVICE       DGC     CX500WDR5 0x31f43321710280c06969f2bf7092f452
disk     48  64000/0xfa00/0x1a  esdisk   CLAIMED     DEVICE       DGC     CX500WDR5 0xf42a447b64f431b99d7fe59f65f71bc7
disk     49  64000/0xfa00/0x1b  esdisk   CLAIMED     DEVICE       DGC     CX500WDR5 0x0e35417912095526b85ff927eeb3051d
disk     50  64000/0xfa00/0x1c  esdisk   CLAIMED     DEVICE       DGC     CX500WDR5 0x9e5630af99c9c6441083ed4b152e8210

---------- Post updated at 01:18 PM ---------- Previous update was at 01:09 PM ----------

Ya know ioscan can probably be made to output all of this but I cannot find a proper manual page for it. I'm guessing here but also hoping -P could take multiple options so that you can have it display all the fields you want instead of just wwid ...