2

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.

marcin
  • 67
  • 1
  • 2
  • 9
  • "Is it possible to instead of 00000 use any 5digit number?" In what context? – n. m. could be an AI Jun 22 '14 at 14:58
  • after some simulation I have many files like: 1_00000_19700101010009_udp_filtered.pcap 3_00000_19700101010009_udp_filtered.pcap 7_00002_19700101010013_udp_filtered.pcap and I would like to make file="${nodeID}_00000_19700101010${ts}_udp_filtered.pcap" working for any 5digid that comes instead of 00000 Like with the extensions of the files. When I put `*.pcap` i can work with any file that has .pcap extension. In my case I would like to work with all the files that instead of 00000 they have 00001 or 00009, whatever – marcin Jun 22 '14 at 15:02
  • "make `file=...` working" ... what's "working"? What are you doing with `file`? What should happen if more than one file matches? – n. m. could be an AI Jun 22 '14 at 15:16

2 Answers2

6

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.

Reinstate Monica Please
  • 11,123
  • 3
  • 27
  • 48
0
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.

NanoDano
  • 1
  • 1
  • `for match in $(ls ${nodeID}_*_19700101010${ts}_udp_filtered.pcap)` --- Never ever use `$(ls ...)`, it's broken and doesn't do what you want it to do. You want `for match in ${nodeID}_*_19700101010${ts}_udp_filtered.pcap`. No `ls`, no `$()`. – n. m. could be an AI Jun 22 '14 at 15:15
  • Can you elaborate on "it's broken and doesn't do what you want it to do"? It works for me and it does what I want it to do. Are there some special edge cases you are referring to? – NanoDano Jun 22 '14 at 15:17
  • Try filenames with spaces in them. Also try a directory matching your pattern with lots of files in it. – n. m. could be an AI Jun 22 '14 at 15:24
  • 1
    @NanoDano More specifically see [this](http://stackoverflow.com/questions/7039130/bash-iterate-over-list-of-files-with-spaces) – Reinstate Monica Please Jun 22 '14 at 15:28