簡體   English   中英

bash中字符串的Grep變量

[英]Grep variable for string in bash

我們想在以下 $test 變量中搜索clid=* (* 代表一個數字):

Welcome to the TeamSpeak 3 ServerQuery interface, type "help" for a list of commands and "help <command>" for information on a specific command.

error id=0 msg=ok

error id=0 msg=ok

cluid=something2384fjdfkj clid=1 name=me

error id=0 msg=ok

最終結果應該是clid=1

使用以下egrep命令:

egrep -o '\bclid=[0-9]+\b' testfile

-o選項,告訴只打印匹配的子字符串

\\b - 詞邊界

這將打印 clid 后跟一個或多個數字:

grep -oP 'clid=\d+' inputfile

或者,如果輸入來自變量,則:

echo  $test  |grep -oP 'clid=\d+'

Bash 有所謂的here 字符串,它允許您將變量的內容提供給 grep,而無需echo或管道:

grep -Eo 'clid=[0-9]+' <<< "$test"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM