所以我有一个日志/文本文件。在该文件中有很多信息,但我只想获取某个字符串。
@@204,clientDisconnect,"apMac"="60:d0:2c:3a:f2:60","clientMac"="8c:45:00:84:e8:98","ssid"="Th","userId"="","wlanId"="240","iface"="wlan32","tenantU,19",,"userName"="d","vlanId"="512","radio"="a/n/ac","encryption"="WPA2-AES","osType"="Android","hostname"="Galaxy-S9","firstAuth"="1580412687","associationTime"="1580411444","ipAssignTime"="1580412688","disconnectTime"="1580412724","sessionDuration"="37","disconnectReason"="8","rxFrames"="26","rxBytes"="4415","txFrames"="93","txBytes"="22693","peakRx"="4415","peakTx"="22693","rssi"="62","receivedSignalStrength"="-43","Instantaneous
我只想要disconnectReason = "8"
.但断开连接的数量可能会根据客户端断开连接的原因而改变。我不确定如何搜索文件并仅输出带有通配符的字符串。
我希望它输出:
disconnectReason ="8"
disconnectReason ="3"
disconnectReason ="1" etc.....
答案1
根据您所说,有很多方法可以解决此问题,这应该足以满足您的需求:
grep -oE '"disconnectReason"="[0-9]+"' yourfile
-o
告诉grep
只返回字符串的匹配部分
答案2
假设您给出的示例是文件中的一行,您可以使用sed
删除您所在字段之前和之后的所有内容:
sed -E 's/.*,("disconnectReason"="[0-9]+"),.*/\1/' <file>
这通过使用带括号的子表达式“记住”您感兴趣的字段和值,然后用于\1
引用第一个(并且仅在这种情况下)记住的子表达式来替换 while 行。
如果您真的坚决要删除 周围的双引号disconnectReason
,我会使用另一个 sed 过滤器来做到这一点,而不是使正则表达式复杂化。它可能效率较低,但更容易阅读:
sed -E 's/.*,("disconnectReason"="[0-9]+"),.*/\1/' <file> |
sed -E 's/"(disconnectReason)"/\1/'