Help with detect with regex and move script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with detect with regex and move script
# 1  
Old 01-17-2017
Help with detect with regex and move script

Hi all,

I am needing some help with a script that will search for a video file by known extensions and then do a pattern search (I'm guessing via regex) and then based on a match of one type of another move the file to an assigned directory.

I would like to do this with either a shell script such as bash or python however, I want to keep it as simple and compact as possible.

Examples

I have a folder that I have downloaded video files to, they are generally a mix of mp4, mkv, avi etc. and some will be movie files of various description and other will be tv episode that will either be named like mytvshow sxxexx (as in the sxx would be season number like s01 and the exx would be episode number like e02 for example) with a video file extension.) so the file may look like the following
TV show;
mytvshow s01e02.mp4
My Movie
my movieName.mkv

So the script needs to look at the extension or use another process to determine it is a video file and then based on the naming convension move the respective video file to a defined folder like

mytvfiles moved to /home/tv
mymovefiles moved to /home/movie

Would the scripting gurus out there help me by pointing me in the right direction with some examples of what i may use and suggestions on what may be best to suit this requirement i.e python or bash or something else maybe?

Thank you in advance for any assistance offered.

Cheers,
Darren.
# 2  
Old 01-17-2017
Dear Simplify/Darren,

I have a few to questions pose in response first:-
  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.



Robin
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 01-17-2017
Hi Robin,

Thanks for taking the time to respond.
Quote:
Originally Posted by rbatte1
Dear Simplify/Darren,

I have a few to questions pose in response first:-
  • Is this homework/assignment? There are specific forums for these.
  • No this is not home or assignment - just something I would like to do to continue to automate my processes.
    Quote:
  • What have you tried so far?
I have been reading some forums and tinkering around with some bash scripts using sed and awk.
Quote:
  • What output/errors do you get?
  • I haven't been successful in getting the right pattern matching working for the tvshows I have been able to bash script the move of the movies without too much problem, however I was hoping to minimise the use of sed or awk and keep it as simple as possible hence the reason I thought I would post here and seek some input from the pros as I am very novice in the Linux scripting environments.
    Quote:
  • What OS and version are you using?
  • Lubuntu 16.10
    Quote:
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
  • scan the download directory, check for video file as there could be apps or docs in the download directory if a video file is found, then check the naming of the video file and then move based on the match of tvshow or movie.
    Quote:
    Most importantly, What have you tried so far?

    There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


    We're all here to learn and getting the relevant information will help us all.
    Robin
    I have only tried to achieve this using bash, however, part of the process is I am calling a python script from another programmer to run some conversion on the video files as well, so I thought it may be more efficient to use python for this process also.

    I am using the manual.py script from GitHub - mdhiggins/sickbeard_mp4_automator: Automatically convert video files to a standardized mp4 format with proper metadata tagging to create a beautiful and uniform media library
    Simplify
    View Public Profile for Simplify
    Find all posts by Simplify
    # 4  
    Old 01-17-2017
    Dear Simplify/Darren,

    It is my pleasure to try to help. It is far more rewarding than the day job (although unpaid) and helps me to learn from others who may make other suggestions or correct me.

    Can you show us your attempts so far? It will help us to steer you so (hopefully) you will understand the evolved solution and be able to support it going forward.

    I would probably write this in ksh/bash like shells, using the find command and variable substitution, but it depends what you know as to where we start making suggestions. There are, of course, many ways to do this.



    Robin
    rbatte1
    View Public Profile for rbatte1
    Visit rbatte1's homepage!
    Find all posts by rbatte1
    # 5  
    Old 01-17-2017
    I have been trying to get this pattern match to work for starters and it is doing my head in....

    Code:
    fileInput=$(printf "$1" | sed -n '/\([sS][0-9][0-9][eE][0-9][0-9]\)/p')
    if (( -z $(printf "$1" | sed -n '/\([sS][0-9][0-9][eE][0-9][0-9]\)/p')  )); then
    echo "yes" 
    else 
    echo "no"
    fi

    So the idea of the above code is to check the input file give to the script as a command line argument and if the pattern matches, we get a yes and if no...

    Also i would ideally like to incorporate some cleaning of the file name here too... so for example

    if the filename input is mytvshow.s01.e02.blah.blah.HDTV.Dolby.mkv i would like to finish with simply mytvshow.s01.e02.mkv

    Does that make sense?

    would be nice if i didn't need to use sed. :-)
    Simplify
    View Public Profile for Simplify
    Find all posts by Simplify
    # 6  
    Old 01-17-2017
    You could try using bash extended globbing to match for example

    Code:
    for filename in \
        show.01x03.mkv \
        S01E2.show.avi \
        S01E2.show.nfo \
        show.S01.E02-03.mp4 \
        movie.720p.DTS.mkv
    do
        ((video=tv=0))
        [[ "$filename" = *@(.mkv|.avi|.mp4) ]] && video=1
        [[ "$filename" = *[Ss]+([0-9])?(.)[Ee]+([0-9])* ]] && tv=1
        [[ "$filename" = *+([0-9])[xX]+([0-9])* ]] && tv=1
    
        printf "%s VIDEO=%d TV=%d\n" "$filename" $video $tv
    done

    output:

    Code:
    show.01x03.mkv VIDEO=1 TV=1
    S01E2.show.avi VIDEO=1 TV=1
    S01E2.show.nfo VIDEO=0 TV=1
    show.S01.E02-03.mp4 VIDEO=1 TV=1
    movie.720p.DTS.mkv VIDEO=1 TV=0

    Extended globbing as described by the bash man page:
    Code:
      ?(pattern-list)   Matches zero or one occurrence of the given patterns
      *(pattern-list)   Matches zero or more occurrences of the given patterns
      +(pattern-list)   Matches one or more occurrences of the given patterns
      @(pattern-list)   Matches one of the given patterns
      !(pattern-list)   Matches anything except one of the given patterns

    Here a pattern-list is a list of items separated by a vertical bar "|" (aka the pipe symbol).
    This User Gave Thanks to Chubler_XL For This Post:
    Chubler_XL
    View Public Profile for Chubler_XL
    Find all posts by Chubler_XL
    # 7  
    Old 01-18-2017
    Fantastic @Chubler_XL - thank you!

    This is very much in line with my thinking/wanted Smilie

    One question how do I change the script to handle if the file names have spaces in the name/string?

    for example, show.01x03 episode name.mkv

    I have another question that i hope we can make simple with a gob

    can we check if both $tv & $video equal 0 then the file is considered a TVShow and if not a Movie?
    something like if ["${tv}" -ne 0 -a "${video}" -ne 0]; then echo "is tv show"; else echo "is movie"; fi so they both have to equal 0 for statement to be true else is false.

    Last edited by Simplify; 01-18-2017 at 11:55 AM.. Reason: Guessed at ICODE tags
    Simplify
    View Public Profile for Simplify
    Find all posts by Simplify
    Login or Register to Ask a Question

    Previous Thread | Next Thread

    10 More Discussions You Might Find Interesting

    1. Shell Programming and Scripting

    How to detect url in use in a script?

    Hello, I have a small script and it runs from web application in below format: pipe:///path_to_myscript.sh url1 url2 url3 myscript.sh: #!/bin/bash count=0 while do count=$((count+1)) exec 3>&1 ((ffmpeg -i $1 ...... -f mpegts pipe:1 2>/dev/null 1>&3 ) 2>&1 | \ while read LINE; do echo... (9 Replies)
    Discussion started by: baris35
    9 Replies

    2. HP-UX

    Script to detect time drift

    Hello there, I am not an expert in networking related stuff but I got a requirement to create UNIX script to query our Company's internal time source via NTP for time drift detect and report it when > +/- 50ms. I have been googling a lot but thought to post it in this forum to get a... (17 Replies)
    Discussion started by: Green_Star
    17 Replies

    3. UNIX for Beginners Questions & Answers

    Script to detect time drift on server

    Hello there, I am not an expert in networking related stuff but I got a requirement to create UNIX script to query our Company's internal time source via NTP for time drift detect and report it when > +/- 50ms. I have been googling a lot but thought to post it in this forum to get a... (1 Reply)
    Discussion started by: Green_Star
    1 Replies

    4. UNIX for Advanced & Expert Users

    A script to detect system type

    Hi forum, So I am trying to determine the OS type with the following script: #!/usr/bin/sh OStype1=`uname -s` Sunos1=SunOs if then echo "This system is Linux" exit 0 elif then echo "This system is SunOs" exit 0 elif (1 Reply)
    Discussion started by: dampio
    1 Replies

    5. Shell Programming and Scripting

    Detect current shell inside a script

    I wish to print my current shell which happens to be bash in my script check.sh more check.sh echo $0 echo `ps -p $$` But instead of printing it prints check.sh i.e the name of the script for both the commands. Can you please suggest how to print the current shell i m on inside the... (2 Replies)
    Discussion started by: mohtashims
    2 Replies

    6. Shell Programming and Scripting

    How to detect Hanged process in shell script?

    I have to check daily 20 processes each day. The names are like Network1 Network2 Network3 ....... Network20. There is built in utility for doing this. Following is the command to check a single network process. check_process_status 1 If we want to check the status of Network2 then the... (6 Replies)
    Discussion started by: Nakul_sh
    6 Replies

    7. Shell Programming and Scripting

    Detect if script starts from queue

    Dear community, what I'm try to do is deny users to run a script without parameters from command bash, but the same script should run without parameters only from crontab. Example runs by crontab:*/5 * * * * /tmp/script.sh Here the normal execution starts every 5 minutes Example #1 runs by... (16 Replies)
    Discussion started by: Lord Spectre
    16 Replies

    8. Shell Programming and Scripting

    Help with auto-detect new files/folders then zip and move script

    Hello, I need a simple script to Auto-detect new files and folders in the directory. And then I need to zip the new files and bzip2 new folders and move them out of that folder where I am detecting changes to the other folder. Remember, I need simple one. If anyone could do it fast, I may... (1 Reply)
    Discussion started by: juzt1s
    1 Replies

    9. Shell Programming and Scripting

    AWK script to detect webpages from file

    Hi guys I'm very new to unix and I have to create an awk script that detects webpage addresses from a file/webpage and outputs how many times each webpage was detected.e.g. if my file was: www.google.com www.facebook.com www.google.com the output should be: www.google.com x2... (2 Replies)
    Discussion started by: ROFL
    2 Replies

    10. UNIX for Dummies Questions & Answers

    how to detect my script is already running

    I have a script which must not be run more than once at any given time. THis script will be scheduled to run every 20 mins as a cron job. In my script can i have logic to say if this script is already running from the previous cron, then exit. How do i go about doing that. If you describe the... (11 Replies)
    Discussion started by: rmulchandani
    11 Replies
    Login or Register to Ask a Question