Reformat multi-line records in perl -
i have commandoutput , want extract lines , join them based on condition. opening file handle command output. below sample data.
array (sas, unused space: 0 mb) logicaldrive 9 (931.5 gb, raid 1, ok) physicaldrive 6c:3:1 (port 6c:box 3:bay 1, sas, 1 tb, ok) physicaldrive 6c:3:2 (port 6c:box 3:bay 2, sas, 1 tb, ok) array k (sas, unused space: 0 mb) logicaldrive 11 (931.5 gb, raid 1, ok) physicaldrive 7c:3:5 (port 7c:box 3:bay 5, sas, 1 tb, ok) physicaldrive 7c:3:6 (port 7c:box 3:bay 6, sas, 1 tb, ok) unassigned physicaldrive 7c:3:7 (port 7c:box 3:bay 7, sas, 1 tb, ok) physicaldrive 7c:3:8 (port 7c:box 3:bay 8, sas, 1 tb, ok) array l (sas, unused space: 0 mb) logicaldrive 12 (931.5 gb, raid 1, ok) physicaldrive 8c:3:5 (port 8c:box 3:bay 5, sas, 1 tb, ok) physicaldrive 8c:3:6 (port 8c:box 3:bay 6, sas, 1 tb, ok)
what i'm trying is:
remove line containing unassigned , physical drives associated it. physical drives in case 2 , 3 or more lines .
join lines containing array , physical drives.
the output should this:
array (sas, unused space: 0 mb) physicaldrive 6c:3:1 (port 6c:box 3:bay 1, sas, 1 tb, ok) physicaldrive 6c:3:2 (port 6c:box 3:bay 2, sas, 1 tb, ok) array k (sas, unused space: 0 mb) physicaldrive 7c:3:5 (port 7c:box 3:bay 5, sas, 1 tb, ok) physicaldrive 7c:3:6 (port 7c:box 3:bay 6, sas, 1 tb, ok)
as said: in case each physical drive might have 2 drives, script should work more lines containing physical drives after array.
below lines of i/p command .
smart array p410i in slot 0 (embedded) (sn: 500143802128a270)
array (sas, unused space: 0 mb) logicaldrive 1 (931.5 gb, raid 1, ok) physicaldrive 2c:1:1 (port 2c:box 1:bay 1, sas, 1 tb, ok) physicaldrive 2c:1:2 (port 2c:box 1:bay 2, sas, 1 tb, ok)
array b (sas, unused space: 0 mb)
logicaldrive 2 (931.5 gb, raid 1, ok) physicaldrive 2c:1:3 (port 2c:box 1:bay 3, sas, 1 tb, ok) physicaldrive 2c:1:4 (port 2c:box 1:bay 4, sas, 1 tb, ok)
array c (sas, unused space: 0 mb)
logicaldrive 3 (931.5 gb, raid 1, ok) physicaldrive 3c:1:5 (port 3c:box 1:bay 5, sas, 1 tb, ok) physicaldrive 3c:1:6 (port 3c:box 1:bay 6, sas, 1 tb, ok)
array d (sas, unused space: 0 mb)
logicaldrive 4 (931.5 gb, raid 1, ok) physicaldrive 3c:1:7 (port 3c:box 1:bay 7, sas, 1 tb, ok) physicaldrive 3c:1:8 (port 3c:box 1:bay 8, sas, 1 tb, ok)
array e (sas, unused space: 0 mb)
logicaldrive 5 (931.5 gb, raid 1, ok) physicaldrive 4c:2:1 (port 4c:box 2:bay 1, sas, 1 tb, ok) physicaldrive 4c:2:2 (port 4c:box 2:bay 2, sas, 1 tb, ok)
array f (sas, unused space: 0 mb)
logicaldrive 6 (931.5 gb, raid 1, ok) physicaldrive 4c:2:3 (port 4c:box 2:bay 3, sas, 1 tb, ok) physicaldrive 4c:2:4 (port 4c:box 2:bay 4, sas, 1 tb, ok)
array g (sas, unused space: 0 mb)
logicaldrive 7 (931.5 gb, raid 1, ok) physicaldrive 5c:2:5 (port 5c:box 2:bay 5, sas, 1 tb, ok) physicaldrive 5c:2:6 (port 5c:box 2:bay 6, sas, 1 tb, ok)
array h (sas, unused space: 0 mb)
logicaldrive 8 (931.5 gb, raid 1, ok) physicaldrive 5c:2:7 (port 5c:box 2:bay 7, sas, 1 tb, ok) physicaldrive 5c:2:8 (port 5c:box 2:bay 8, sas, 1 tb, ok)
array (sas, unused space: 0 mb)
logicaldrive 9 (931.5 gb, raid 1, ok) physicaldrive 6c:3:1 (port 6c:box 3:bay 1, sas, 1 tb, ok) physicaldrive 6c:3:2 (port 6c:box 3:bay 2, sas, 1 tb, ok)
array j (sas, unused space: 0 mb)
logicaldrive 10 (931.5 gb, raid 1, ok) physicaldrive 6c:3:3 (port 6c:box 3:bay 3, sas, 1 tb, ok) physicaldrive 6c:3:4 (port 6c:box 3:bay 4, sas, 1 tb, ok)
array k (sas, unused space: 0 mb)
logicaldrive 11 (931.5 gb, raid 1, ok) physicaldrive 7c:3:5 (port 7c:box 3:bay 5, sas, 1 tb, ok) physicaldrive 7c:3:6 (port 7c:box 3:bay 6, sas, 1 tb, ok)
unassigned
physicaldrive 7c:3:7 (port 7c:box 3:bay 7, sas, 1 tb, ok) physicaldrive 7c:3:8 (port 7c:box 3:bay 8, sas, 1 tb, ok)
enclosure sep (vendor id hp, model hp sas exp card) 248 (wwid: 5001438020b5cc65, box: 3)
expander 250 (wwid: 5001438020b5cc66, box: 1)
sep (vendor id pmcsiera, model src 8x6g) 249 (wwid: 500143802128a27f)
one approach take - , first though - set record separator in perl $/
, process each record regular expressions.
something maybe:
#!/usr/bin/env perl use strict; use warnings; #set record separator linefeed followed word array. #if place "array" occurs, linefeed superfluous. local $/ = "\narray"; while ( <data> ) { #remove word 'array'. s/\s*array\s*//g; #delete after 'unassigned' in array. s/unassigned.*//gms; #remove 'logicaldrive' lines. s/.*logicaldrive.*//g; #delete linefeeds in record s,\n,,gms; #tidy of spaces. s,\s+, ,g; s,^\s*,,g; #print record. print; print "\n"; } __data__ array (sas, unused space: 0 mb) logicaldrive 9 (931.5 gb, raid 1, ok) physicaldrive 6c:3:1 (port 6c:box 3:bay 1, sas, 1 tb, ok) physicaldrive 6c:3:2 (port 6c:box 3:bay 2, sas, 1 tb, ok) array k (sas, unused space: 0 mb) logicaldrive 11 (931.5 gb, raid 1, ok) physicaldrive 7c:3:5 (port 7c:box 3:bay 5, sas, 1 tb, ok) physicaldrive 7c:3:6 (port 7c:box 3:bay 6, sas, 1 tb, ok) unassigned physicaldrive 7c:3:7 (port 7c:box 3:bay 7, sas, 1 tb, ok) physicaldrive 7c:3:8 (port 7c:box 3:bay 8, sas, 1 tb, ok) array l (sas, unused space: 0 mb) logicaldrive 12 (931.5 gb, raid 1, ok) physicaldrive 8c:3:5 (port 8c:box 3:bay 5, sas, 1 tb, ok) physicaldrive 8c:3:6 (port 8c:box 3:bay 6, sas, 1 tb, ok)
Comments
Post a Comment