
類似主題:連續將終端輸出寫入文字文件
我在 ubuntu mate 上有一個 USB RF 接收器,它可以向終端提供此類數據:
b';311;'
b';312;'
b';312;00000000;036;552;1014f49;3020;2659;6294;1049;2659;S;'
b';313;'
我可以只保存最長的那一個嗎?最好沒有“b';xxx”,但我也可以稍後解析它。
答案1
您需要將射頻資料傳輸到一個可以進行過濾的程式中,然後傳輸到一個檔案中
cat | ./filter.pl < /dev/ttyusb0 >> output.file
filter.pl
你猜對了,過濾:
#!/usr/bin/env perl
# disable buffering
$|=1;
$re = qr/
^ # Match start of line
b'; # b quote semi colon
\d+ # Match one or more digits
; # semi colon
(.+) # One or more characters, store the match as $1
;' # semi colon, single quote
/x;
while (<>) {
print "$1\n" if $_ =~ $re
}
正規表示式匹配您的格式並提取最後兩個分號之間的字元。
答案2
在 中sed
,使用基本正規表示式:
whatever | sed -n "s/^b';[0-9]\{3\}];\(.\+\);'/\1/p" >> output_file
在 中sed
,使用擴展正規表示式:
whatever | sed -n "s/^b';[0-9]{3};(.+);'/\1/p" >> output_file