marcin
marcin

Reputation: 67

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

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.

Upvotes: 2

Views: 12224

Answers (2)

NanoDano
NanoDano

Reputation: 1

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.

Upvotes: 0

Reinstate Monica Please
Reinstate Monica Please

Reputation: 11603

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.

Upvotes: 6

Related Questions