bash 如何在字符串中间使用通配符?猛击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24352431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use wildcard in the middle on the string? bash
提问by marcin
I am writing a bash script
我正在写一个 bash 脚本
My files are like:
file="${nodeID}_00000_19700101010${ts}_udp_filtered.pcap"
. Is it possible to instead of 00000
use any 5digit number? I thought about using
我的文件是这样的:
file="${nodeID}_00000_19700101010${ts}_udp_filtered.pcap"
. 是否可以代替00000
使用任何 5 位数字?我想过使用
file="${nodeID}_
*_19700101010${ts}_udp_filtered.pcap"
file="${nodeID}_
*_19700101010${ts}_udp_filtered.pcap"
sometimes I have 00001, sometimes 00004, etc.
有时我有 00001,有时是 00004,等等。
回答by Reinstate Monica Please
Something like
就像是
echo "${nodeID}"_[0-9][0-9][0-9][0-9][0-9]_19700101010"${ts}"_udp_filtered.pcap
Note, *
and [something]
won't expand in quotes, so only the variables are quoted above.
注意,*
并且[something]
不会在引号中展开,所以上面只引用了变量。
回答by NanoDano
for match in ${nodeID}_*_19700101010${ts}_udp_filtered.pcap
do
#echo $match
#do something with $match
#file=$match
done
This will find all the matches in the directory and allow you to do something with them. The asterisk will match anystring, not just 5 digits. You can be more specific in the regex, but if you know the format of the filenames, you can determine whether the * is safe or needs to be more specific.
这将找到目录中的所有匹配项,并允许您对它们执行某些操作。星号将匹配任何字符串,而不仅仅是 5 位数字。您可以在正则表达式中更具体,但如果您知道文件名的格式,则可以确定 * 是否安全或需要更具体。