LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   REGEX from array, returns no matches. (https://www.linuxquestions.org/questions/linux-server-73/regex-from-array-returns-no-matches-816009/)

dr_strangelove 06-23-2010 05:37 PM

REGEX from array, returns no matches.
 
Hello,

I'm attempting to use REGEX to isolate media (video and audio) essence that relate to alias/reference Quicktime movies. The Quicktime movie filename (excluding ".mov" can always be found in the media-essence filename.

Stages of my process are:
  • I cache the contents of a text file into an array ($file). This text file/array contains a list of Quicktime movies and media essence. Essence for each Quicktime movie is contained in this text file. An example text file is below:
    Code:

    MOVIE1.mov
    media.dir/MOVIE1.m2v
    media.dir/MOVIE1.wav
    media.dir/MOVIE1_1.wav
    media.dir/MOVIE1_2.wav
    media.dir/MOVIE1_3.wav
    MOVIE2.mov
    media.dir/MOVIE2.m2v
    media.dir/MOVIE2.wav
    media.dir/MOVIE2_1.wav
    media.dir/MOVIE2_2.wav
    media.dir/MOVIE2_3.wav
    MOVIE3.mov
    media.dir/MOVIE3.m2v
    media.dir/MOVIE3.wav
    media.dir/MOVIE3_1.wav
    media.dir/MOVIE3_2.wav
    media.dir/MOVIE3_3.wav

  • I isolate which items in the list are Quicktime movies (simply REGEX matching ".mov" and cache these in another array for later use in the script.
  • Each item within the Quicktime-movie array ($mov_array) is used as search criteria when searching the remainder of the file list.
  • I plan on using all items which match the Quicktime mov filename (pre ".mov") to generate ffmpeg tasks.

My problem is that the following statement
Code:

if [[ $essence =~ $id ]]
returns no results.


If I manually intervene and state that
Code:

#id="SOMETHINGOROTHER"
then I get results.

Extracted code is below:

Code:

#!/bin/bash

#This script loops through a text file and creates an ffmpeg string to wrap this essence into a single container.

number_mov=0
array=()
mov_array=${#array[*]}
regex1="mov"
topdir="/Users/Dev/1107613"
subdir="/media.dir/"

cd $topdir

IFS='
'
file=( $( < MOVIES.txt ) )                                                                        # Here we grab the contents of the text file within the folder
for (( i = 0 ; i < ${#file[@]} ; i++ ))                                                # Here we loop through its contents and check for the presence of each line
        do
        a=${file[$i]}
        if [[ $a =~ $regex1 ]]; then                                                        # If we match regex1 (normally, is it a mov?) then ...
    mov_array=(${mov_array[@]} "$a")                                                # ... add this to the array of movs
        number_mov=$((number_mov+1))
        fi
done

for mov in ${mov_array[@]}                                                                        # Loop through our MOV array ...
do
        id=$(echo $mov|sed 's/.mov//g')                                                        # ... remove the file extension, remember, only movs should be in this array!
        echo 'Media ID -' $id
        for essence in ${file[@]}                                                                # Loop through our MOV array ...
                do       
                        #id="MOVIE1"
                        echo "Checking"
                        echo $id
                        echo $essence
                        if [[ $essence =~ $id ]]
                                then
                                  echo "Match"
                        else
                                echo "Unlucky"
                        fi
                        echo
                done
done

Many thanks in advance and apologies if I'm missing something bleedingly obvious.

dr_strangelove

NB* This script is being developed on Mac OS X 10.5.8, for eventual deployment on Linux version 2.6.27.7-9-pae (geeko@buildhost) (gcc version 4.3.2 [gcc-4_3-branch revision 141291] (SUSE Linux) )

rweaver 06-23-2010 05:54 PM

Code:


IFS='
'
file=( $( < tt.txt.fil ) )                                                                      # Here we grab the contents of the text file within the folder
for (( i = 0 ; i < ${#file[@]} ; i++ ))                                        # Here we loop through its contents and check for the presence of each line
        do
        a=${file[$i]}
        if [[ $a =~ $regex1 ]]; then                                                    # If we match regex1 (normally, is it a mov?) then ...
        mov_array=(${mov_array[@]} "$a")                                                # ... add this to the array of movs
        number_mov=$((number_mov+1))
        fi
done

for mov in ${mov_array[@]}                                                                      # Loop through our MOV array ...
do
        id=$(echo $mov|sed 's/.mov//g')                                                # ... remove the file extension, remember, only movs should be in this array!
        #echo 'Media ID -' $id
        for essence in ${file[@]}                                                              # Loop through our MOV array ...
                do
                        echo "id:$id essence:$essence mov:$mov\n"
                        if [[ $essence =~ $id ]]
                                then
                                echo "essence: $essence\n"
                        else
                                w00t=0
                        fi
                done
done

Which returns...
Code:

id:MOVIE1 essence:MOVIE1.mov mov:MOVIE1.mov
essence: MOVIE1.mov
id:MOVIE1 essence:media.dir/MOVIE1.m2v mov:MOVIE1.mov
essence: media.dir/MOVIE1.m2v
id:MOVIE1 essence:media.dir/MOVIE1.wav mov:MOVIE1.mov
essence: media.dir/MOVIE1.wav
id:MOVIE1 essence:media.dir/MOVIE1_1.wav mov:MOVIE1.mov
essence: media.dir/MOVIE1_1.wav
id:MOVIE1 essence:media.dir/MOVIE1_2.wav mov:MOVIE1.mov
essence: media.dir/MOVIE1_2.wav
id:MOVIE1 essence:media.dir/MOVIE1_3.wav mov:MOVIE1.mov
essence: media.dir/MOVIE1_3.wav
id:MOVIE1 essence:MOVIE2.mov mov:MOVIE1.mov
id:MOVIE1 essence:media.dir/MOVIE2.m2v mov:MOVIE1.mov
id:MOVIE1 essence:media.dir/MOVIE2.wav mov:MOVIE1.mov
id:MOVIE1 essence:media.dir/MOVIE2_1.wav mov:MOVIE1.mov
id:MOVIE1 essence:media.dir/MOVIE2_2.wav mov:MOVIE1.mov
id:MOVIE1 essence:media.dir/MOVIE2_3.wav mov:MOVIE1.mov
id:MOVIE1 essence:MOVIE3.mov mov:MOVIE1.mov
id:MOVIE1 essence:media.dir/MOVIE3.m2v mov:MOVIE1.mov
id:MOVIE1 essence:media.dir/MOVIE3.wav mov:MOVIE1.mov
id:MOVIE1 essence:media.dir/MOVIE3_1.wav mov:MOVIE1.mov
id:MOVIE1 essence:media.dir/MOVIE3_2.wav mov:MOVIE1.mov
id:MOVIE1 essence:media.dir/MOVIE3_3.wav mov:MOVIE1.mov
id:media.dir/MOVIE1.m2v essence:MOVIE1.mov mov:media.dir/MOVIE1.m2v
id:media.dir/MOVIE1.m2v essence:media.dir/MOVIE1.m2v mov:media.dir/MOVIE1.m2v
essence: media.dir/MOVIE1.m2v
<SNIP>

So that being said, im unsure what exactly your problem is.

dr_strangelove 06-23-2010 06:30 PM

Thanks for your super swift response rweaver

The text file example fortuitously, includes media essence and Quicktime movies in sequential order - this is not guaranteed. The ultimate aim is to be able to associate each $id to all the $essence that it relates. Whilst my script is ultimately flawed somehow, I'd like the output to be as below, where a 'Match' is indicated where (emboldened) and non-matched and also highlighted (italicised) ... I will later include a little logic to use these 'switches' to create ffmpeg commands.

Code:

Media ID - MOVIE1
Checking
MOVIE1
MOVIE1.mov
Match

Checking
MOVIE1
media.dir/MOVIE1.m2v
Match

Checking
MOVIE1
media.dir/MOVIE1.wav
Match

Checking
MOVIE1
media.dir/MOVIE1_1.wav
Match

Checking
MOVIE1
media.dir/MOVIE1_2.wav
Match

Checking
MOVIE1
media.dir/MOVIE1_3.wav
Match

Checking
MOVIE1
MOVIE2.mov
Unlucky

Checking
MOVIE1
media.dir/MOVIE2.m2v
Unlucky

... ETC ...

Thanks for your continuing patience.

dr_strangelove


All times are GMT -5. The time now is 10:58 AM.