[英]How to parse a report using Shell script?
我有一個命令行程序,可以打印出如下報告:
I found 0 problems on your database,
0 problems were found on your database
等等。
我想寫一個僅將數字導出到變量的linux shell腳本。 我找不到正確的方法。 我懷疑我沒有正確使用grep
(或者有更好的命令); 即
I found 43 problems on your database,
43 problems were found on your database
$VAR=43
請指教
$ var=$(echo -e "I found 43 problems on your database,
43 problems were found on your database" | grep -om 1 '[0-9]\+')
$ echo $var
43
我發現Lev Levitsky的解決方案是完美的,但我只是想補充一點,以防萬一您的報告中包含許多帶有數字的此類行,並且您希望處理它們中的每行,則將使用類似以下的內容:
cat report | grep -o '[0-9]\+' | while read var
do
# do something with var
done
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.