我正在撰寫一個 bash 腳本,該腳本在每次從系統觸發通知時執行命令,并且我正在使用dbus-monitor "interface='org.freedesktop.Notifications'".
我正在努力從該命令的輸出中僅提取標題和訊息,您能幫我這樣做嗎?
我試著做:
dbus-monitor "interface='org.freedesktop.Notifications'" |
grep --line-buffered "string" |
grep --line-buffered -e method -e ":" -e '""' -e urgency -e notify -v |
grep --line-buffered '.*(?=string)|(?<=string).*' -oPi |
grep --line-buffered -v '^\s*$' |
grep --line-buffered -wvi 'image-data\|variant\|desktop-entry\|activate\|default\|sender-pid' |
while read -r LINE; do
printf "$LINE\n\n"
# ...other commands...
done
它列印出如下內容:
"Firefox"
"<message from Firefox>"
"Firefox"
"Firefox"
// ...
所以這不完全是我想要完成的!
任何幫助表示贊賞!
謝謝
編輯:
的例子dbus-monitor:
signal time=1668013439.676960 sender=org.freedesktop.DBus -> destination=:1.1820 serial=4294967295 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameLost
string ":1.1820"
method call time=1668013457.635273 sender=:1.131 -> destination=:1.56 serial=704 path=/org/freedesktop/Notifications; interface=org.freedesktop.Notifications; member=Notify
string "Firefox"
uint32 0
string ""
string "<message1>"
string "<message2>"
array [
string "default"
string "Activate"
]
array [
dict entry(
string "desktop-entry"
variant string "Firefox"
)
dict entry(
string "image-data"
variant struct {
int32 48
int32 48
int32 192
boolean true
int32 8
int32 4
array of bytes [
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
...
]
}
)
]
int32 -1
我想提取:
Title: "Firefox"
Message: <message1>, <message2>
Time: <date of receiving>
uj5u.com熱心網友回復:
你可以使用 GNUawk來完成這個任務:
dbus-monitor "interface='org.freedesktop.Notifications'" |
awk '
$1 == "signal" {
if (state) emit()
state = 0
_title = ""
_messages = ""
split($2,arr,"=")
_date = strftime("%Y-%m-%dT%H:%M:%S%z",arr[2])
next
}
$1 == "method" { state = 1; next; }
$1 == "string" && (state == 1 || state == 2) && match($0,/". "/) {
s = substr($0,RSTART,RLENGTH)
if (state == 1) {
_title = s
state = 2
next
} else
_messages = (_messages == "" ? "" : _messages ", ") s
}
$1 == "array" { state = 3 }
END { if (state) emit() }
function emit() {
print "Title:", _title
print "Message:", _messages
print "Date: " _date
fflush()
}
'
備注:輸入格式對我來說不清楚,因此當前狀態機可能不準確;它適用于該示例
Title: "Firefox"
Message: "<message1>", "<message2>"
Date: 2022-11-09T12:03:59-0500
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/532842.html
標籤:重击总线
