質問

I am getting output of some command like

ymmetrix ID      : 000190105233

Host Name         : 1CHT-AMS-002-D

Identifiers Found : 200a03000000002c
                    200a03000000003c

Device  Cap(MB)  Attr  Dir:P
------  -------  ----  ----
05AC          3         9A:0
05AD          3         9A:0
05B2          3         8A:0
05B3          3         8A:0
0A0B      69713   (M)   8A:0, 9A:0
0A0F      69713   (M)   8A:0, 9A:0
0A13      69713   (M)   8A:0, 9A:0
0A17      69713   (M)   8A:0, 9A:0
0A1B      69713   (M)   8A:0, 9A:0

-----------------------------

MB Total: 348577
GB Total:  340.4

I need help on getting the output like

Device |   Dir:P          |  Identifiers Found
------ |   ----           |  -----------------
05AC   |    9A:0          |   200a03000000002c
05AD   |    9A:0          |   200a03000000003c
05B2   |    8A:0          |
05B3   |    8A:0          |
0A0B   |    8A:0, 9A:0    |
0A0F   |    8A:0, 9A:0    |
0A13   |    8A:0, 9A:0    |
0A17   |    8A:0, 9A:0    |
0A1B   |    8A:0, 9A:0    |

where "Identifiers" can be more than two,

I am using this

for i in 'cat text |grep -i Identifiers';
do
cat text |awk '{print $1,$4,$5,$i}';
done

but not getting "Identifiers" in third column.

Immediate help will be appreciated.

役に立ちましたか?

解決

You can try:

awk -f f.awk input.txt

where input.txt is you input data file, and f.awk is:

/Identifiers Found/{
    id[++j]=$4
    while($1 != "") {
        getline
        id[++j]=$1
    }
}

/^Device/ {
    getline
    getline
    while ($1 != "") {
        i++
        if (NF==3) {
            dev[i]=$1
            dir[i]=$3
        }
        else {
            dev[i]=$1
            dir[i]=$4" "$5
        }
        getline
    }
}

END {
    if (i>j) n=i
    else n=j
    fmt="%-7s |   %-12s|  %-17s\n"
    printf fmt, "Device", "Dir:P", "Identifiers Found"
    printf fmt, "-----", "-----",  "-----------------"
    for (i=1; i<=n; i++)
        printf fmt, dev[i],dir[i],id[i]
}

他のヒント

Using awk

awk '
    BEGIN {
        p="%-10s|%-15s|%s\n"
        printf p,"Device","Dir:P","Identifiers Found"
        printf p,"------","-----","-----------------"}
    /Identifiers/ {g=1}
    /Device/{g=0}
    /----------------/{f=0}
    g {a[++i]=substr($0,21,16)} 
    f {printf p,$1,$4 " " $5,a[++j]}
    /------  -------/ {f=1}
    ' file
Device    |Dir:P          |Identifiers Found
------    |-----          |-----------------
05AC      |               |200a03000000002c
05AD      |               |200a03000000003c
05B2      |               |
05B3      |               |
0A0B      |8A:0, 9A:0     |
0A0F      |8A:0, 9A:0     |
0A13      |8A:0, 9A:0     |
0A17      |8A:0, 9A:0     |
0A1B      |8A:0, 9A:0     |
          |               |
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top