bash中字符串的Grep变量
我们想在以下$test变量中搜索bash中字符串的Grep变量,bash,grep,Bash,Grep,我们想在以下$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 c
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
欢迎使用TeamSpeak 3服务器查询界面,键入“help”可查看命令列表,键入“help”可查看特定命令的信息。
错误id=0消息=ok
错误id=0消息=ok
cluid=something2384fjdfkj clid=1 name=me
错误id=0消息=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在这里称为字符串,它允许您将变量的内容提供给grep,而无需回音或管道:
grep -Eo 'clid=[0-9]+' <<< "$test"
grep-Eo'clid=[0-9]+'