Hi,
This has been pestering me for quite a while, any help will be highly appreciated
The current directory has a file with below name
npidata_20050523-20171210.csv
The below wildcard matched the above file
ls -ltr npidata_????????-201712??.csv
But when the part '201712' is put into a variable and use in the file name somehow does not match the file name
ls -ltr npidata_?????????${yyyymm}??.csv
Is there a way I can get around this ?
I cannot reproduce this:
$ touch npidata_20050523-20171210.csv
$ ls -ltr npidata_????????-201712??.csv
-rw-r--r-- 1 scrutinizer staff 0 Jan 6 20:49 npidata_20050523-20171210.csv
$ yyyymm=201712
$ ls -ltr npidata_?????????${yyyymm}??.csv
-rw-r--r-- 1 scrutinizer staff 0 Jan 6 20:49 npidata_20050523-20171210.csv
I did notice you used a ?
instead of a -
, but since a ?
matches a -
that is still a match.
$ ls -ltr npidata_????????-${yyyymm}??.csv
-rw-r--r-- 1 scrutinizer staff 0 Jan 6 20:49 npidata_20050523-20171210.csv
What shell are you using?
In addition to what Scrutinizer already asked...
What command (exactly) did you use to set the the variable yyyymm
?
I am using ksh and for the variable, I hard coded the value
yyyymm=20171210
That would be appropriate if the variable had been named yyyymmdd
and you had used the filename matching pattern:
ls -ltr npidata_?????????${yyyymmdd}.csv
instead of:
ls -ltr npidata_?????????${yyyymm}??.csv
For the filename matching pattern you're using, you needed to set yyyymm
using:
yyyymm=201712
instead of:
yyyymm=20171210
well the actual reason of doing it this way was because I do not know the day part of the date which is why the pattern matching
OK, but there is a mismatch this way so you would need to adapt the date. You could for example cut off the last two digits:
$ yyyymmdd=20171210
$ ls -ltr npidata_?????????${yyyymmdd%??}??.csv
-rw-r--r-- 1 scrutinizer staff 0 Jan 6 20:49 npidata_20050523-20171210.csv
$
1 Like