How to use wildcard in the middle on the string? bash

孤街浪徒 提交于 2021-01-28 01:26:33

问题


I am writing a bash script

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}_*_19700101010${ts}_udp_filtered.pcap"

sometimes I have 00001, sometimes 00004, etc.


回答1:


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.




回答2:


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 any string, 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.



来源:https://stackoverflow.com/questions/24352431/how-to-use-wildcard-in-the-middle-on-the-string-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!