LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-23-2010, 05:37 PM   #1
dr_strangelove
LQ Newbie
 
Registered: Jun 2010
Location: London, UK
Posts: 10

Rep: Reputation: 0
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) )

Last edited by dr_strangelove; 06-23-2010 at 05:43 PM.
 
Old 06-23-2010, 05:54 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
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.
 
Old 06-23-2010, 06:30 PM   #3
dr_strangelove
LQ Newbie
 
Registered: Jun 2010
Location: London, UK
Posts: 10

Original Poster
Rep: Reputation: 0
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

Last edited by dr_strangelove; 06-23-2010 at 06:32 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash replace all matches of regex substring in string nickleus Linux - General 3 04-30-2011 11:08 AM
grep regex . matches new lines?! lambchops468 Linux - Newbie 3 03-24-2008 09:19 PM
How to pass array to a function in Java so that it returns changed? kornerr Programming 1 09-19-2006 12:16 PM
Regex Question: Only print part of line that matches TheMeteorPolice Programming 5 01-12-2006 01:21 PM
bash: routine outputting both matches and non-matches separately??? Bebo Programming 8 07-19-2004 06:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 01:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration