Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Regex doesn't work using loop to go through filenames

regex shell

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1 chelfc

chelfc

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 30 September 2014 - 09:27 AM

I've been working on this for a while now and I can't seem to crack it. I have in a folder some files

testing.test.S09E01.720p.HDTV.x264-TWIST
dmscript.sh

I have the script in there to test it before I set it up with download manager to run once an episode has been downloaded.

The script has the following code.
 

#!/bin/sh
#THIS SCRIPT WILL DETERMINE TV SHOWS AND EPISODES AND MOVE THEM TO THE CORRECT
#FOLDER WHICH WILL ALLOW EITHER SICKBEARD OR COUCHPOTATO TO RENAME AND MOVE THEM

regex="([Ss]?([0-9]{1,2})[-|.]?[x|Ee|]?(\d{2})|(0?\d{1})(\d{2}))"

#where the tv shows will be copied to
tvdir="/volume1/public/NZB/Complete/tv/processed/"

#this was a debug variable to allow me to see where the code fails
num="1"

#will change the * to full path of folder later, for now this works because script is running from
#inside folder
for filename in *
do
        if [[ "$filename =~ $regex ]]
        then
        #display the current filename and the variable number
        echo "$filename $num"
        #commented on the following code so when the script works I will just uncomment
        #mv $filename $tvdir
        #change variable to 2, this is to see whether the if test will fail and skip the file
        #that doesn't conform to the regex
        num="2"
        else
        echo "nothing of use"
        
done

However, once I run the code, I get this

testing.test.S09E01.720p.HDTV.x264-TWIST 1
dmscript.sh 2

obviously something goes wrong, as I just want it to display the first in the list above and ignore the other.

I got the regex rule from http://regex101.com/r/qZ2eO9/1 , I ignored the /gim at the end as I am unsure whether this will work in shell, and just stuck with the Ss and Ee so it isn't case sensitive



#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 30 September 2014 - 11:36 AM

Shouldn't you check for num="1" in your if statement?


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 chelfc

chelfc

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 30 September 2014 - 03:43 PM

I put num="1" inside the if statement, but then it lists everything but with a 1 at the end of it, i.e.

 

testing.test.S09E01.720p.HDTV.x264-TWIST 1
dmscript.sh 1

 

I've tried many different variations of the regex, nothing seems to be working



#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 01 October 2014 - 05:09 AM

if [[ "$filename =~ $regex ] && [num="1"]]


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 01 October 2014 - 06:57 AM

Without trying it out myself, I think there are 2 things:

 

You are missing the close quote on the left side of the if statement.

 

Second, try to put the regex in brackets:

 

if [[ $filename =~ [ $regex ] ]] 

 

Again, I'm flying blind here, so I can't test it myself, sorry.


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth