0

I have a directory full of files with the name template

xxx[0-9][0-9][0-9[0-9]ss(wrx-mmm).zip

The zip files contain a file yrq000.dat and I don't want to overwrite them when the files are uncompressed.

I am creating a processing script but I want to name that common file a name associated with its source zip and I'm checking the method:

for f in $(ls xxx*)
do
  uniquename="${f%%(wrx.*zip}"
done

I can't figure out how to strip that opening parens from the string.

Is that possible with the bash string manipulation the way I am doing it? That is to say, it does not work. What do I need to add or remove so that it does work?

  • What exactly is the problem here? Consider adding example input and the corresponding desired output. – igal Apr 17 at 23:14
1

Try removing the dot from your pattern, i.e.:

for f in xxx*;
do
    uniquename="${f%%(wrx*zip}";
    echo "${uniquename}";
done

That takes files whose names are of the form xxx[0-9][0-9][0-9[0-9]ss(wrx-mmm).zip and converts them to strings of the form xxx[0-9][0-9][0-9[0-9]ss. Is that what you're trying to accomplish?

  • Yeah that did it. I see that I was using the "." as though it were a regex, when in fact it is taken as literal. – Ken Ingram Apr 17 at 23:31
  • 2
    Bash Pitfall #1 – steeldriver Apr 18 at 0:15
  • Interesting. I typically used expansion to select the "for" range and then when I saw "$()" was a tool I began using it. I guess I made some incorrect assumptions about why my loops wood break when filenames had whitespace or non-standard characters. – Ken Ingram Apr 18 at 1:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.