我有一个输出(一个众所周知的btmon
工具),如下例所示,我需要解析它并从下面获取一些信息:如果UUID出现在{{1然后它等于HCI event:
,然后取32f9169f-4feb-4883-ade6-1f0127018db3
和Address:
字段的值并将它们放在一起并创建一个新行。例如,
A0:E6:F8:48:EF:78 B7
A0:E6:F8:48:F1:AF B7
所以这是我需要以所描述的方式解析的流或文件:
RSSI:
答案 0 :(得分:1)
尝试关注awk并告诉我这是否有帮助。
awk '/HCI Event/{A=1;next} /Address:/{val=$2} /32f9169f-4feb-4883-ade6-1f0127018db3/ && A{print val;val=A=""}' Input_file
现在也添加非单线形式的解决方案。
awk '/HCI Event/{
A=1;
next
}
/Address:/ {
val=$2
}
/32f9169f-4feb-4883-ade6-1f0127018db3/ && A{
print val;
val=A=""
}
' Input_file
<强> 编辑:2 强>
awk '/HCI Event/{A=1;next} /Address:/{val=$2} /32f9169f-4feb-4883-ade6-1f0127018db3/{A++} /RSSI/ && A==2{print val,toupper(substr($0,length($0)-2,2));A=""}' Input_file
<强> OR 强>
awk '/HCI Event/{
A=1;
next
}
/Address:/ {
val=$2
}
/32f9169f-4feb-4883-ade6-1f0127018db3/{
A++
}
/RSSI/ && A==2{
print val,toupper(substr($0,length($0)-2,2));
A=""
}
' Input_file
答案 1 :(得分:1)
这个脚本应该完成这项工作:
#!/bin/bash
inputfile="path/to/file.txt"
match=false
id=32f9169f-4feb-4883-ade6-1f0127018db3
while read -r line;
do
if (! echo ${line} | grep -q "HCI\|Address\|RSSI\|${id}"); then
continue
fi
if echo ${line} | grep -q "> HCI Event:"; then
if ${match}; then
echo "${address} ${rssi:3:2}"
fi
address=""
rssi=""
match=false
continue
fi
if echo ${line} | grep -q "Address: ..:..:..:..:..:.."; then
address=`echo ${line} | grep -o "..:..:..:..:..:.."`
continue
fi
if echo ${line} | grep -q "RSSI: .*dBm"; then
rssi=`echo ${line} | grep -o "(0x..)"`
continue
fi
if echo ${line} | grep -q "${id}"; then
match=true
continue
fi
done < "$inputfile"
if ${match}; then
echo "${address} ${rssi:3:2}"
fi
答案 2 :(得分:1)
awk 解决方案:
awk -v uid="32f9169f-4feb-4883-ade6-1f0127018db3" '/Address:/{ addr=$2 }
/UUIDs/ && (getline nl > 0) && nl~uid{ uuid=nl; f=1 }
/RSSI/{ if(f) { rssi=toupper(substr($4,4,2)); print addr,rssi; f=0 } }' file
输出:
A0:E6:F8:48:EF:78 B7
A0:E6:F8:48:F1:AF B7