[英]bash awk - how to extract single characters from this line
我從lmstat輸出有以下幾行:
Users of ccmpsuite: (Total of 2 licenses issued; Total of 1 license in use)
"ccmpsuite" v2016.03, vendor: cdlmd
floating license
<username> <nodename> /dev/tty (v2015.02) (<hostname>/1999 436), start Fri 7/17 21:41
我想以某種方式從第一行中提取“ 2”和“ 1”,以獲取已頒發的許可證總數和正在使用的許可證總數,以在其他地方使用它。 據我所知,lmstat不支持任何最小文本或xml輸出。 我知道我可以使用以下方法來整行:
awk '/^[ ]*Users of[ ]*/'
我將如何提取“ 1”和“ 2”?
您可以使用grep -oP
來獲得兩個計數:
grep -oP '\d+(?= license)' file
2
1
由於您用awk標記了問題:
awk '/Users of ccmpsuite:/{print $6,$11}' input.txt
2 1
重擊:
#!/bin/bash
while read line
do
if [[ $line =~ "Users of ccmpsuite:" ]]
then
match=($line)
echo ${match[5]}
echo ${match[10]}
fi
done < input.txt
Awk不會捕獲正則表達式分組,但是perl會捕獲:
perl -n -e'/Total of (\d+) licenses/ && print $1'
將打印許可證數量:
perl -n -e'/Total of (\d+) license/ && print $1'
將打印正在使用的許可證數量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.