0

Please explain me how I should accomplish the following using awk:

I need to create a mirror of the 1st two 146G drives on a raid controller. If i show the list of empty drives I get this result:

Smart Array P420 in Slot 5

unassigned

physicaldrive 1I:1:1 (port 1I:box 1:bay 1, SAS, 146 GB, OK)
physicaldrive 1I:1:2 (port 1I:box 1:bay 2, SAS, 146 GB, OK)
physicaldrive 1I:1:4 (port 1I:box 1:bay 4, SAS, 900.1 GB, OK)
physicaldrive 2I:1:5 (port 2I:box 1:bay 5, SAS, 146 GB, OK)
physicaldrive 2I:1:6 (port 2I:box 1:bay 6, SAS, 146 GB, OK)
physicaldrive 2I:1:8 (port 2I:box 1:bay 8, SAS, 900.1 GB, OK)

For this example I put the list in a file free-drives and then create command. I want to get output like so:

drives=1I:1:1,1I:1:2

My 1st try is

drives=$(awk '/146 GB/ { printf $2"," ;}; END{print ;}' free-drives)

but that results in

1I:1:1,1I:1:2,2I:1:5,2I:1:6,

I couldn't find a proper way to get rid of the trailing comma so I used sed to get rid of that [1].

drives=$(awk '/146 GB/ { printf $2"," ;}; END{print ;}' free-drives|sed -e 's/,$//g')

Still we have 4 disks, I need two. So I tried some tips I've read in 5316572, like this one:

drives=$(awk 'a-->0;/146 GB/{ printf $2"," ;}{a=2}; END{print ;}' free-drives|sed -e 's?,$??g')

but this yields a complete mess.

So, what is the correct way of doing this with only one awk oneliner?

[1]: If you have any advise for me how to fix this: That would be great too ;-).

Community
  • 1
  • 1
Alex
  • 3
  • 2

2 Answers2

0

This should do that:

awk '/146 GB/{t=t $2 ","}; END{sub(/,$/, "", t); print t}' free-drives

Output:

1I:1:1,1I:1:2,2I:1:5,2I:1:6
konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • Thanx Konsolebox, but that command seems not to do exactly what i want: when I test, the results looks like so: 1I:1:11I:1:22I:1:52I:1:6 – Alex Sep 13 '13 at 14:33
0

this one-liner should give what you want:

 awk '/146 GB/{x++;s=s?s","$2:$2}x==2{print s;exit}' free-drivers

with your input, it outputs:

1I:1:1,1I:1:2

I think you know how to assign it to variable, so I just skip that part.

Kent
  • 189,393
  • 32
  • 233
  • 301