Mouseover script help?

Shell script filename pattern matching help

  • Another shell script help needed post I previously posted a question (answered quickly and succesfully - thanks!) about filtering out TIFF files from a directory tree that have no matching PDF associated with them. (http://ask.metafilter.com/193064/Need-a-shell-script-to-filter-out-directories) Now I've been trying to modify that script without success to do the following: Find all PDF files in a directory tree that do not have a matching (that is, filename match) with an additional set of characters in the filename. The characters are: _Aug11 So, if I have a set of files in a directory: one.pdf one_Aug11.pdf two.pdf three.pdf three_Aug11.pdf then the file "two.pdf" has no corresponding "two_Aug11.pdf". I need the script to echo this filename out (or to a file, whatever). I tried using the original script but obviously when you find all .pdf files, you match the _Aug11 ones also. So the script would report that one_Aug11.pdf has no matching one_Aug11_Aug11.pdf which is not what I want. Am I looking at using awk to solve this? I am not clear on how to use awk witihin a bash script. Help!

  • Answer:

    for fn in *.pdf; do if [[ $fn != *_Aug11.pdf && ! -e ${fn//.pdf/_Aug11.pdf} ]]; then echo $fn; fi; done

dukes909 at Ask.Metafilter.Com Visit the source

Was this solution helpful to you?

Other answers

You can use grep to filter out the "_Aug11" files. Here's a basic example, which will work as long as none of the files have spaces in their filenames: date_pattern="_Aug11" ls $directory | grep -v $date_pattern | while read filename do     base=${filename%.pdf}     if [ ! -f "${base}${date_pattern}.pdf" ]     then         echo $filename     fi done

1970s Antihero

Wrote a quick Perl script http://www.sourcepod.com/cdmwmu08-5566 that might be a bit more flexible.

Blazecock Pileon

Wow! Again super fast responses. The one liner by Rat Spatula worked great, will try others. Note: some will have spaces in their names.

dukes909

Ok, I'm missing it: what does this part of the script do? -e ${fn//.pdf/_Aug11.pdf}

dukes909

For names with spaces, the variable expansions will need surrounding doublequotes in places where it's important: for fn in *.pdf; do if [[ "$fn" != *_Aug11.pdf && ! -e "${fn//.pdf/_Aug11.pdf}" ]]; then echo $fn; fi; done -e http://www.gnu.org/s/bash/manual/bash.html#Bash-Conditional-Expressions for a file's existence. The // modifier http://www.gnu.org/s/bash/manual/bash.html#Shell-Parameter-Expansion one chunk of a variable's expanded value with another.

Rat Spatula

This brings up another question. What if I wanted to assign each of the files (one.pdf and one_Aug11.pdf) a variable name? Is this possible with the // modifier? Ultimately what I'm trying to do is if there is a one.pdf and an one_Aug11.pdf then I want to remove the one.pdf and rename the one_Aug11.pdf to just one.pdf (or just mv one_Aug11.pdf one.pdf ) ...

dukes909

Spend some time playing around with the various operators described at my "replaces" link above. This kind of string-chopping is fairly easy with BASH.

Rat Spatula

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.