Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I would like to sort the output of multipath -ll into a csv type of file using bash script. For example # multipath -ll would give the following output

360060e80056fc30000006fc30000513c dm-41 HITACHI,OPEN-V
 \_ 4:0:0:45 sdcm 69:160 [active][ready]
 \_ 3:0:0:45 sdcn 69:176 [active][ready]
360060e80056fc30000006fc300005162 dm-31 HITACHI,OPEN-V
 \_ 3:0:0:36 sdbu 68:128 [active][ready]
 \_ 4:0:0:36 sdbv 68:144 [active][ready]
360060e80056fc30000006fc300005127 dm-56 HITACHI,OPEN-V
 \_ 3:0:0:6  sdg  8:96   [active][ready]
 \_ 4:0:0:6  sdt  65:48  [active][ready]

What I would like is to create a csv file that would be in this format

LUN ID no., SCSI DEVICE1, SCSI DeVICE2
360060e80056fc30000006fc300005127,sdg,sdt
360060e80056fc30000006fc300005162,sdbu,sdbv

I am just looking for a basic idea of how it could be done, not the entire script. Any help would be greatly appreciated.

share|improve this question

1 Answer 1

up vote 2 down vote accepted
> awk '/^[^ ]/ {printf "\n%s",$1; next}; { printf ",%s",$3; }' file

360060e80056fc30000006fc30000513c,sdcm,sdcn
360060e80056fc30000006fc300005162,sdbu,sdbv
360060e80056fc30000006fc300005127,sdg,sdt
share|improve this answer
    
thanks a lot, exactly what I need. Could you please explain what it does so that I can modify it in the future if I need to? –  Linuxnoob339 Dec 17 '14 at 15:06
    
@Linuxnoob339 This command treats lines beginning with a space and the other ones differently. From the lines without leading space the first element is printed (after a newline because the newline is not printed at the end). From the other lines the third element is printed (after a comma). Using printf instead of print avoids the trailing newline. –  Hauke Laging Dec 17 '14 at 15:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.