1

I have 2 commands that output the devices on my linux system:

$ devlist -t csv -o device,vol_name
device,vol name,
/dev/mapper/mpath62,volume1,
/dev/mapper/mpath63,my_volume,
/dev/mapper/mpath64,foo,
/dev/mapper/mpath65,bar,
/dev/mapper/mpath66,2nd_vol,
$ multipathd -k'show maps'
name     sysfs uuid             
mpath62  dm-0  20017380029ab0043
mpath65  dm-1  20017380029ab0044
mpath66  dm-2  20017380029ab0045
mpath63  dm-3  20017380029ab0046
mpath64  dm-4  20017380029ab0047

How can I merge the output to print (preferably, w/o creating temporary files):

device,vol name,
/dev/dm-0,volume1,
/dev/dm-3,my_volume,
/dev/dm-4,foo,
/dev/dm-1,bar,
/dev/dm-2,2nd_vol,

Thanks

1 Answer 1

1

You can use awk with process substitution:

awk -F '[/, ]+' 'FNR==NR{a[$1]=$2; next}
   FNR==1{print;next}
   $4 in a{printf "/%s/%s,%s,\n", $2, a[$4], $(NF-1)}
' <(multipathd -k'show maps') <(devlist -t csv -o device)
device,vol name,
/dev/dm-0,volume1,
/dev/dm-3,my_volume,
/dev/dm-4,foo,
/dev/dm-1,bar,
/dev/dm-2,2nd_vol,
Sign up to request clarification or add additional context in comments.

5 Comments

It only prints the first field for me (w/o the volume1, my_volume, etc...). How can I print any extra fields if there are in the original output?
Since I don't have these command available, I could only test with the output provided and that is giving the shown output. Are you getting exact same output as shown? Try: awk -F '[/,[:blank:]]+' at the start.
You're right. I accidently omitted a comma after the second field. Updated original post.
Works! Thanks! How can I print any additional fields if I choose to add to the devlist command?
All the fields from devlist commands are available from $2 onwards. You can just add few more fields in printf above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.