bash根据另一个命令的输出替换命令输出的一部分

时间:2015-03-05 14:51:06

标签: linux bash

我有2个命令在我的linux系统上输出设备:

$ 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

如何合并输出以进行打印(最好不要创建临时文件):

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,

由于

1 个答案:

答案 0 :(得分:1)

您可以将awkprocess 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,