LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-09-2015, 04:34 PM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Question how to stop egrep Pattern in Bash Script from catching what it is not suppose to?


I am going to try and make this as understandable as possiable,

I am using a script that goes through files looking at the directory name when it finds one with the pattern it then acts on it within a 'if statment' the pattern is either in caps or lower case or mixed caps and lower case. Only two letters and number CD x. when I run this on the command line it works perfecly. but in the script it will grab a name that is does not even have any of the chars in the pattern then use it as if it does off of the 'if statment'. Now I will show you the code.

what this code does is off of a loop getting the path/fileName.ext then splits it up. looking for the ext to match 'mp3' if it matches then it looks to see what the name of that directory is that contains the mp3. If the name is CD x of any type Cdx cDx cdx , the x being a number. Then it will pull back one more directory to get the name of that directory to use instead for a metadata tag in the Album Tag, and not the CDx. It is written so that it does not matter how deep the tree goes it steps back one more directory no matter what to get the name of that directory, instead of the directory that is named CDx (CD 1 - CD 2 etc.)



Code:
   find /path -type f -name "*.*" | while [ x -lt y ] ;
     do read FILENAME;


	 
	.....
	
	
	if [[ "$ext" == 'mp3' ]] ; then

		AlbumDir=${path##*/}
		
		string="$( echo "$AlbumDir" | egrep -o [C-c][D-d][0-9])"

		

		echo "[[ "$AlbumDir" == *"$string" ]]"
		
		if [[ "$AlbumDir" == *"$string" ]] ; then

			nPath=${path%/*}

			echo "foundCD : $AlbumDir"

			NGetDirN=${nPath##*/}

			AlbumDir="$NGetDirN"
			
			echo "AlbumDir : $AlbumDir"
                        # returns dir one step back from the mp3 file
			 
		else
                         # returns dir with the mp3 in it
			echo "NO CD DIR : $AlbumDir"
			 
		fi
	fi
	......
	done


the results everything is echo'ed we see the complete path. In this examle we can see that it did what it is suppose to, nine directories down is the CDx directory then one back, being the two steps is the name of the directory to be used,

Code:
                                                                             #<-- back   forward -->
/media/data/dynamic-dir/baceAlbumName/New/New1/New2/New3/New4/New5/New6/New7/New8/New9/CD 2/2 Eden.mp3

xpath : /media/data/dynamic-dir/baceAlbumName/New/New1/New2/New3/New4/New5/New6/New7/New8/New9/CD 2
xbase : 2 Eden.mp3
xfext : mp3
xpref : 2 Eden


path  : /media/data/dynamic-dir/baceAlbumName/New/New1/New2/New3/New4/New5/New6/New7/New8/New9/CD 2
pref  : 2 Eden
ext   : mp3
4444444444444444444444444444
[[ CD 2 == * ]]
foundCD : CD 2
AlbumDir : New9
RRRRRRRRRRRRRRRRRRRRRRR
where this file lays within the directory named "New" it still catches it as saying CDx, having typed this all out step by step to mimic the complete code on the command line it returns empty here within the script it grabs the name "New" as being CDx

Code:
/media/data/dynamic-dir/baceAlbumName/New/3 Desire.mp3

xpath : /media/data/dynamic-dir/baceAlbumName/New
xbase : 3 Desire.mp3
xfext : mp3
xpref : 3 Desire

path  : /media/data/dynamic-dir/baceAlbumName/New
pref  : 3 Desire
ext   : mp3
 
[[ New == * ]]
foundCD : New
AlbumDir : baceAlbumName
if i hard code the string

Code:
string2="CD"
this will work every time except when it comes across a directory named Cd1 - Cd2 etc...

hence me trying that pattern egrep. what bothers me is that this works on the command line.

Code:
$ E="Cd1 oere"
$ A="$( echo "$E" | egrep -o [C-c][D-d][0-9])"
$ echo "$A"
Cd1

-----------

$ E="New"
$ A="$( echo "$E" | egrep -o [C-c][D-d][0-9])"
$ echo "$A"
[RETURNS BLANK SPACE - emptyness]

------
[userx@voided scripts]$ E="New/Dog"
[userx@voided scripts]$ A="$( echo "$E" | egrep -o [C-c][D-d][0-9])"
[userx@voided scripts]$ echo "$A"
[Empty Return / blank line]
Any suggestions?

Thanks,
 
Old 12-09-2015, 04:42 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Don't use ranges when you only want two chars - locale messes with range real bad.
Code:
[Cc][Dd][0-9]

Last edited by syg00; 12-09-2015 at 04:43 PM. Reason: typo
 
Old 12-09-2015, 05:21 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by syg00 View Post
Don't use ranges when you only want two chars - locale messes with range real bad.
Code:
[Cc][Dd][0-9]
Thought that worked , tried even this http://wiki.bash-hackers.org/syntax/pattern


Code:
string2="$( echo "$AlbumDir" | egrep -o '( CD | cd | Cd )'  )"

## that will pick up CD 20 , CD 100, Cd 2 etc... but even with that and trying this on the IF statment

[ "$string" == *"$subString"* ] 

[ "$string" == *"$subString" ] 


[ "$string" == "$subString"* ] 


[ "$string" == "$subString" ]
i still get this

Code:
/media/data/dynamic-dir/baceAlbumName/New/3 Desire.mp3

xpath : /media/data/dynamic-dir/baceAlbumName/New
xbase : 3 Desire.mp3
xfext : mp3
xpref : 3 Desire


path  : /media/data/dynamic-dir/baceAlbumName/New
pref  : 3 Desire
ext   : mp3
4444444444444444444444444444
[[ New ==  ]]
foundCD : New
AlbumDir : baceAlbumName
RRRRRRRRRRRRRRRRRRRRRRR
its still picking up New as CD

Last edited by BW-userx; 12-09-2015 at 05:40 PM.
 
Old 12-09-2015, 06:10 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
That wasn't what I wrote.
 
Old 12-09-2015, 08:00 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by syg00 View Post
That wasn't what I wrote.
Sorry I didn't put up the resultes from what you wrote I just shared a different way I tired it after I tried yours

from what you wrote

Code:
echo "4444444444444444444444444444"
	
	
	if [[ "$ext" == 'mp3' ]] ; then
		AlbumDir=${path##*/}
		
		string2="$( echo "$AlbumDir" | egrep -o [Cc][Dd][0-9] )"
		
		echo
		echo "string2 $string2"
		echo
		
		string="CD"
		echo "[[ "$AlbumDir" == "$string2" ]]"
		
		if [[ "$AlbumDir" == "$string2" ]] ; then
			nPath=${path/*}
			echo "foundCD : $AlbumDir"
			NGetDirN=${nPath##*/}
			AlbumDir="$NGetDirN"
			
			echo "AlbumDir : $AlbumDir"
			 
		else
			echo "NO CD DIR : $AlbumDir"
			 
		fi
	fi
	
	echo "RRRRRRRRRRRRRRRRRRRRRRR"
with CD

Code:
/media/data/dynamic-dir/baceAlbumName/New/New1/New2/CD 3/I Wanna Be Your Man **Bonus Track** - L. A. Guns.mp3

xpath : /media/data/dynamic-dir/baceAlbumName/New/New1/New2/CD 3
xbase : I Wanna Be Your Man **Bonus Track** - L. A. Guns.mp3
xfext : mp3
xpref : I Wanna Be Your Man **Bonus Track** - L. A. Guns


path  : /media/data/dynamic-dir/baceAlbumName/New/New1/New2/CD 3
pref  : I Wanna Be Your Man **Bonus Track** - L. A. Guns
ext   : mp3
4444444444444444444444444444

string2 

[[ CD 3 ==  ]]
NO CD DIR : CD 3
RRRRRRRRRRRRRRRRRRRRRRR
without CD

Code:
/media/data/dynamic-dir/baceAlbumName/New/3 Desire.mp3

xpath : /media/data/dynamic-dir/baceAlbumName/New
xbase : 3 Desire.mp3
xfext : mp3
xpref : 3 Desire


path  : /media/data/dynamic-dir/baceAlbumName/New
pref  : 3 Desire
ext   : mp3
4444444444444444444444444444

string2 

[[ New ==  ]]
NO CD DIR : New
RRRRRRRRRRRRRRRRRRRRRRR
on the egrep side of things it returns empty and seems to work now, If I modify it further like this - by stripping of the ${VAR[0-9]*]} -- then add the star * to the left side of the condtional statment this is what I get, But I think now it is a empty space that is causing it to not match when it has a 'CD' . 'Cd' within the dir name, because it is really
Code:
[[ 'CD ' == 'CD' ]]
which returns a false - not true .

its still not perfectly working -- it's late ,, I'll peck at it more tomorrow

Code:
	echo "4444444444444444444444444444"
	
	
	if [[ "$ext" == 'mp3' ]] ; then
		AlbumDir=${path##*/}
		
		string2="$(echo -e "$AlbumDir" | egrep -o [Cc][Dd])"
		
		echo
		echo "string2 "$string2""
		echo
		
		string="CD"
		echo "[[ "${AlbumDir[0-9]*}" == "$string2" ]]"
		
		echo "--> ${AlbumDir[0-9]*}"
		echo
		
		if [[ "${AlbumDir%[0-9]*}" == "$string2" ]] ; then
			nPath=${path%/*}
			echo "foundCD : $AlbumDir"
			NGetDirN=${nPath##*/}
			AlbumDir="$NGetDirN"
			
			echo "AlbumDir : $AlbumDir"
			 
		else
			echo "NO CD DIR : $AlbumDir"
			 
		fi
	fi
	
	echo "RRRRRRRRRRRRRRRRRRRRRRR"

Code:
/media/data/dynamic-dir/baceAlbumName/New/3 Desire.mp3

xpath : /media/data/dynamic-dir/baceAlbumName/New
xbase : 3 Desire.mp3
xfext : mp3
xpref : 3 Desire


path  : /media/data/dynamic-dir/baceAlbumName/New
pref  : 3 Desire
ext   : mp3
4444444444444444444444444444

string2 

[[ New ==  ]]
--> New

NO CD DIR : New
RRRRRRRRRRRRRRRRRRRRRRR

pref : 3 Desire

track Number : 3



fCount : 38

aCount 40

/media/data/dynamic-dir/baceAlbumName/New/New1/New2/CD 3/I Wanna Be Your Man **Bonus Track** - L. A. Guns.mp3

xpath : /media/data/dynamic-dir/baceAlbumName/New/New1/New2/CD 3
xbase : I Wanna Be Your Man **Bonus Track** - L. A. Guns.mp3
xfext : mp3
xpref : I Wanna Be Your Man **Bonus Track** - L. A. Guns


path  : /media/data/dynamic-dir/baceAlbumName/New/New1/New2/CD 3
pref  : I Wanna Be Your Man **Bonus Track** - L. A. Guns
ext   : mp3
4444444444444444444444444444

string2 CD

[[ CD  == CD ]]
--> CD 

NO CD DIR : CD 3
RRRRRRRRRRRRRRRRRRRRRRR

OK I Tinkered with it somemore before tomorrow
this is what I got and it is a lot more now

their has to be a shorter way to do this, not having to use so much code to strip the white spaces off, or catch the pattern, it getting to look too complecated now

(I'm just starting with this stuff btw, string malnipulation and BASH )



Code:
echo "4444444444444444444444444444"
	
	
	if [[ "$ext" == 'mp3' ]] ; then
		AlbumDir=${path##*/}
		
		string2="$(echo -e "$AlbumDir" | egrep -o [Cc][Dd])"
		
		echo
		echo "string2 "$string2""
		echo
		
		string="CD"
		
		
		
		echo
		AlbumDir="$(echo -e "${AlbumDir%[0-9][0-9][0-9]}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
		echo "1 - [[ "${AlbumDir}" == "$string2" ]]"
		AlbumDir="$(echo -e "${AlbumDir%[0-9][0-9]}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
		echo "2 - [[ "${AlbumDir}" == "$string2" ]]"
		AlbumDir="$(echo -e "${AlbumDir%[0-9]}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
		echo "3 - [[ "${AlbumDir}" == "$string2" ]]"
		
		
		echo
		echo "--> ${AlbumDir}"
		echo "4 - [[ "${AlbumDir}" == "$string2" ]]"
		if [[ "${AlbumDir}" == "$string2" ]] ; then
			nPath=${path%/*}
			echo "foundCD : $AlbumDir"
			NGetDirN=${nPath##*/}
			AlbumDir="$NGetDirN"
			
			echo "AlbumDir : $AlbumDir"
			 
		else
			echo "NO CD DIR : $AlbumDir"
			 
		fi
	fi
	
	echo "RRRRRRRRRRRRRRRRRRRRRRR"
this is a lot for each instance of a patteren of numbers -- this removes the spaces on the sides of what ever it is pipped to it - I strip off the numbers within it before pipping it to sed
Code:
		echo
		AlbumDir="$(echo -e "${AlbumDir%[0-9][0-9][0-9]}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
		
                 echo "1 - [[ "${AlbumDir}" == "$string2" ]]"
		
                AlbumDir="$(echo -e "${AlbumDir%[0-9][0-9]}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
		
                echo "2 - [[ "${AlbumDir}" == "$string2" ]]"
		
                AlbumDir="$(echo -e "${AlbumDir%[0-9]}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
		
                echo "3 - [[ "${AlbumDir}" == "$string2" ]]"
results: this catches CD 1 Cd1 Cd 20 CD 12 CD 101 etc

Code:
/media/data/dynamic-dir/Cd 101/Over The Wall - Echo & The Bunnymen.mp3

xpath : /media/data/dynamic-dir/Cd 101
xbase : Over The Wall - Echo & The Bunnymen.mp3
xfext : mp3
xpref : Over The Wall - Echo & The Bunnymen


path  : /media/data/dynamic-dir/Cd 101
pref  : Over The Wall - Echo & The Bunnymen
ext   : mp3
4444444444444444444444444444

string2 Cd


1 - [[ Cd == Cd ]]
2 - [[ Cd == Cd ]]
3 - [[ Cd == Cd ]]

--> Cd
4 - [[ Cd == Cd ]]
foundCD : Cd
AlbumDir : dynamic-dir
RRRRRRRRRRRRRRRRRRRRRRR
OK, My Brain just kicked in a little bit -- coffee burn out maybe getting to me

this is what I got it down to - I just caught it use two %% to get the farthest to the left of patteren [0-9] being one number - then strip the rest of it off along with it.

Code:
AlbumDir="$(echo -e "${AlbumDir%%[0-9]*}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
echo "3 - [[ "${AlbumDir}" == "$string2" ]]"
this might work that I am going to put more into testing tomorrow for sure ---

sorry if you found it a lot to read,, it seems to be more thinking out loud then anything right now -- hope someome can learn from it though

Last edited by BW-userx; 12-09-2015 at 09:14 PM.
 
Old 12-10-2015, 01:58 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
I have come into this a bit late, but I have to say I am more than a little lost

Now I am not sure if the complete script does more to other file types, ie. other than mp3, but if not, why does the find not just find mp3's?
Code:
find /path -type f -name "*.*"

find /path -type f -iname "*.mp3"
You will notice I also included the use of 'iname' as it seems you cannot be sure of case

The next part that confuses me is where you say you have a successful run. Just looking at the path returned:
Code:
/media/data/dynamic-dir/baceAlbumName/New/New1/New2/New3/New4/New5/New6/New7/New8/New9/CD 2/2 Eden.mp3
Here we see the directory name we are looking at in red, but it does not match your grep at all:
Code:
egrep -o [C-c][D-d][0-9]
Whilst I agree that the use of ranges is wrong here, the intent is to match, cdn, Cdn, CDn, cDn. The problem is that your directory has a space in it so none of these will ever match.
Yet you advise this was a successful run??

Now as far as I can see, none of your future versions change this so I am at a loss at the reason for using grep at all here??

Personally I would also not bother with grep at all and just use bash's pattern matching.

Another line that doesn't make any sense to me is:
Code:
AlbumDir=${path##*/}
Now maybe you did something to path prior to where I am seeing it, but if it is the path returned by find, this variable would now store the name of the mp3

Sorry for the confusion, but if you could provide more detail I may be able to help?
 
Old 12-10-2015, 09:57 AM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by grail View Post
I have come into this a bit late, but I have to say I am more than a little lost
Now I am not sure if the complete script does more to other file types, ie. other than mp3, but if not, why does the find not just find mp3's?
Code:
find /path -type f -name "*.*"

find /path -type f -iname "*.mp3"
It seemed redundant to have that - either way with the
Code:
 if [[ "$ext" == 'mp3' ]]
it does not matter one way or the other it will still catch the mp3's files having
Code:
 find -type f -name "*.mp3"
just cuts back on search time. though I did have it like this
Code:
find -type f -name "*.mp3"
repeating the if statment catches it, in my real world script that I will use this in I will be using the
Code:
*.*
to work with other files within the dir structor too at the same time within a different loop within the whole script loop. jpg. png, txt etc ...

Quote:
Originally Posted by grail View Post
You will notice I also included the use of 'iname' as it seems you cannot be sure of case

The next part that confuses me is where you say you have a successful run. Just looking at the path returned:
Code:
/media/data/dynamic-dir/baceAlbumName/New/New1/New2/New3/New4/New5/New6/New7/New8/New9/CD 2/2 Eden.mp3
Here we see the directory name we are looking at in red, but it does not match your grep at all:
Code:
egrep -o [C-c][D-d][0-9]
Whilst I agree that the use of ranges is wrong here, the intent is to match, cdn, Cdn, CDn, cDn. The problem is that your directory has a space in it so none of these will ever match.
Yet you advise this was a successful run??
The spaces I did make mention of later on within all of that way "too much to read" that I posted in here.
Quote:
on the egrep side of things it returns empty and seems to work now, If I modify it further like this - by stripping of the ${VAR[0-9]*]} -- then add the star * to the left side of the condtional statment this is what I get, But I think now it is a empty space that is causing it to not match when it has a 'CD' . 'Cd' within the dir name, because it is really
Code:

[[ 'CD ' == 'CD' ]]
Quote:
Originally Posted by grail View Post

Now as far as I can see, none of your future versions change this so I am at a loss at the reason for using grep at all here??
Personally I would also not bother with grep at all and just use bash's pattern matching.
cuz I am still learning what "items" are within Linux to use when working with strings, as i am seen many different ways that people show to get the same results using sed, grep, egrep awk etc.

Quote:
Originally Posted by grail View Post
Another line that doesn't make any sense to me is:
Code:
AlbumDir=${path##*/}
Now maybe you did something to path prior to where I am seeing it, but if it is the path returned by find, this variable would now store the name of the mp3
Yes that is what the ..... are for to indecate other code prior to and after that sniplet I posted.

Code:
c=$FILENAME
xpath=${c/*} 
xbase=${c##*/}
xfext=${xbase##*.}
xpref=${xbase%.*}
path=${xpath}
pref=${xpref}
ext=${xfext}
the
Code:
path
gives the complete path to the file, minus the file name, I just use that to strip off the furthest / from the left leaving the directory name to look at to see if it says any combination of CD possiable to computer aided human choice (hopefully). By using this
Code:
${path##*/}
Quote:
Originally Posted by grail View Post
Sorry for the confusion, but if you could provide more detail I may be able to help?
now that I have responded to everything you wrote to me about, I left off at dealing with the spaces within a directory name, that you made mention of by using this code: if you get confused by what I have written just GOTO line 10 (MODED) make that LINE 11
Code:
.................
echo "4444444444444444444444444444"
	
	if [[ "$ext" == 'mp3' ]] ; then

		AlbumDir=${path##*/}
		
		string2="$(echo -e "$AlbumDir" | egrep -o [Cc][Dd])"
		
		echo
		echo "string2 "$string2""
		echo
		
		string="CD"
				
		AlbumDir="$(echo -e "${AlbumDir%%[0-9]*}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
		echo "1 - [[ "${AlbumDir}" == "$string2" ]]"
				
		echo
		echo "--> ${AlbumDir}"
		echo "2 - [[ "${AlbumDir}" == "$string2" ]]"
		echo
		
		if [[ "${AlbumDir}" == "$string2" ]] ; then

			nPath=${path%/*}

			echo "foundCD : $AlbumDir"

			NGetDirN=${nPath##*/}

			AlbumDir="$NGetDirN"
			
			echo "AlbumDir : $AlbumDir"
			 
		else
			echo "NO CD DIR : $AlbumDir"
			 
		fi
	fi
	
	echo "RRRRRRRRRRRRRRRRRRRRRRR"

.............
LINE 10 1/2

Let me brake it down for you (and others). I first check for mp3 for good mesure,
Code:
 if [[ "$ext" == 'mp3' ]] ; then
then check to see if the directory name has the CD pattern within it by using egrep so that no matter how it is written as long as the CD part is not seperated by looking just for the pattern of CD with no spaces between the letters, and by leaving off any numbers that maybe there. I then use this code, modified per you pointing out to me (thank you very much ) that it is better to remove the ' - ' then have it in there
Code:
string2="$(echo -e "$AlbumDir" | egrep -o [Cc][Dd])"
that insures (hopefully) that the search pattern will always be one form of CD or the other first. Then I take the name of the directory again then by removing any numbers off of the dir that maybe there as well by doing this
Code:
AlbumDir="$(echo -e "${AlbumDir%%[0-9]*}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
the
Code:
AlbumDir="$(echo -e "${AlbumDir%%[0-9]*}"
takes the furtest to the left from the right side that is a number then removes the number along with everything else to the right of it. Now that all of the numbers are striped off it. I then pipes it to sed that removes any white spaces that maybe on either side
Code:
sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
I do both side for the just in case, and to just be safe senerio. The whole thing put together giving me the results below.

Results:
Code:
/home/userx/JunkMusic1/Cd 101/The Puppet - Echo & The Bunnymen.mp3

xpath : /home/userx/JunkMusic1/Cd 101
xbase : The Puppet - Echo & The Bunnymen.mp3
xfext : mp3
xpref : The Puppet - Echo & The Bunnymen


path  : /home/userx/JunkMusic1/Cd 101
pref  : The Puppet - Echo & The Bunnymen
ext   : mp3
4444444444444444444444444444
path : /home/userx/JunkMusic1/Cd 101
path##*/ Cd 101


string2 Cd

AlbumDir after striping number, and removing spaces
Cd

1 - [[ Cd == Cd ]]

--> Cd
2 - [[ Cd == Cd ]]

foundCD : Cd
AlbumDir : JunkMusic1
RRRRRRRRRRRRRRRRRRRRRRR
LINE 10

using egrep to get the search string with whatever form of CD is used in the directory name
Code:
string2="$(echo -e "$AlbumDir" | egrep -o [Cc][Dd])"
giving me this
Code:
string2 : Cd
then with AlbumDir again I now remove the number(s) off of it too, and spaces on either side of it,
Code:
AlbumDir="$(echo -e "${AlbumDir%%[0-9]*}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
giving me this
Code:
Cd
then it checks it within the if statment
Code:
1 - [[ Cd == Cd ]]
 --> Cd 
foundCD : Cd
the path is this: /home/userx/JunkMusic1/Cd 101
then using this code to Change the name of the AlbumDir to one more dir back off of 'CD x' dir name I do this,
Code:
nPath=${path%/*}
NGetDirN=${nPath##*/}
AlbumDir="$NGetDirN"
using the results for path from this
Code:
c=$FILENAME
xpath=${c%/*} 
xbase=${c##*/}
xfext=${xbase##*.}
xpref=${xbase%.*}
path=${xpath}
pref=${xpref}
ext=${xfext}
which gives me this -
Code:
AlbumDir : JunkMusic1
RRRRRRRRRRRRRRRRRRRRRRR
one directory back off from the directory that has the pattern I am looking for. It seems to work with any pattern of likeness of, CD xxx cd xxx cdxxx CDxxxxxxxx etc...

So, now it looks like I have a working chunck of code.

if you think you have a much better way in doing this please share it with me.
thanks !

LINE 11


--------- FurtherMore -- Now that I have wasted your time and mine, by writting all of what you just read and posting it, I then tested it again using my complete $HOME dir searching it enterly -- I got back a few false readings -- therefore it is not a working solution ..

fCount is amount of files looked at
aCount is amount of files to look at
Code:
working_dir="$HOME"

aCount="$(find "$working_dir" -type f -name "*.mp3" | wc -l)"

find "$working_dir" -type f -name "*.mp3" | while [ $fCount -lt $aCount ] ; 
	do read FILENAME;
results
Code:
fCount : 163

aCount 48177

/home/userx/JunkkTreeMusic/1988 - Vixen (320)/06 - Vixen - One Night Alone.mp3

xpath : /home/userx/JunkkTreeMusic/1988 - Vixen (320)
xbase : 06 - Vixen - One Night Alone.mp3
xfext : mp3
xpref : 06 - Vixen - One Night Alone


path  : /home/userx/JunkkTreeMusic/1988 - Vixen (320)
pref  : 06 - Vixen - One Night Alone
ext   : mp3
4444444444444444444444444444
path : /home/userx/JunkkTreeMusic/1988 - Vixen (320)
path##*/ 1988 - Vixen (320)

AlbumDir=1988 - Vixen (320)


string2 

AlbumDir after striping number, and removing spaces


1 - [[  ==  ]]

--> 
2 - [[  ==  ]]

foundCD : 
AlbumDir : JunkkTreeMusic
RRRRRRRRRRRRRRRRRRRRRRR
for some reason it thought this and ones like this have the CD pattern within the dir. it looks like it matched on "blanks" in both $var's. Either more code is neeeded as in NULLing the vars then chekcing for NULL too, or spaces in the $vars then bypassing them, or a better way to insure the it does not pick up spaces off of the
Code:
string2="$(echo -e "$AlbumDir" | egrep -o [Cc][Dd])"
and the
Code:
AlbumDir="$(echo -e "${AlbumDir%%[0-9]*}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
------------------- late night update ---

grep a CD pattern the run it through a case statment ---

Code:
string2="$(echo -e "$AlbumDir" | grep -o [Cc][Dd])"
#removes leading white space on both ends of string
string2="$(echo -e "${string2}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
Code:
		string2="$(echo -e "$AlbumDir" | grep -o [Cc][Dd])"
		#removes leading white space on both ends of string
		string2="$(echo -e "${string2}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
	 
		echo;echo;echo;echo
		echo "going into Case Statment"
		echo
		 case $string2 in
    "CD" ) nPath=${path%/*}
			echo "foundCD : $AlbumDir"
			NGetDirN=${nPath##*/}
			AlbumDir="$NGetDirN"
			echo "AlbumDir : $AlbumDir"
			sleep 3
        ;;
    "cd" ) nPath=${path%/*}
			echo "foundCD : $AlbumDir"
			NGetDirN=${nPath##*/}
			AlbumDir="$NGetDirN"
			echo "AlbumDir : $AlbumDir"
			sleep 3
        ;;
    "Cd" ) nPath=${path%/*}
			echo "foundCD : $AlbumDir"
			NGetDirN=${nPath##*/}
			AlbumDir="$NGetDirN"
			echo "AlbumDir : $AlbumDir"
			sleep 3
        ;;
     "Disk" ) nPath=${path%/*}
			echo "foundCD : $AlbumDir"
			NGetDirN=${nPath##*/}
			AlbumDir="$NGetDirN"
			echo "AlbumDir : $AlbumDir"
			sleep 3
			;;
	"disk" ) nPath=${path%/*}
			echo "foundCD : $AlbumDir"
			NGetDirN=${nPath##*/}
			AlbumDir="$NGetDirN"
			echo "AlbumDir : $AlbumDir"
			sleep 3
			;;
			
    * ) echo "NO CD DIR : $AlbumDir"
        
    esac

Last edited by BW-userx; 12-10-2015 at 06:22 PM.
 
Old 12-11-2015, 05:04 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
See if this helps at all, I did change some variable names just to make it a little clearer:
Code:
start_path='/home/userx/JunkMusic1/Cd 101/The Puppet - Echo & The Bunnymen.mp3'
path=${start_path/*}
ext=${start_path##*.}
match='^cd'

echo "4444444444444444444444444444"

if [[ "$ext" == 'mp3' ]] ; then

  CD_Dir=${path##*/}

  if [[ "${CD_Dir,,}" =~ $match ]] ; then

    echo "foundCD : $CD_Dir"

    nPath=${path%/*}

    AlbumDir=${nPath##*/}

    echo "AlbumDir : $AlbumDir"

  else
    echo "NO CD DIR : $CD_Dir"

  fi  
fi

echo "RRRRRRRRRRRRRRRRRRRRRRR"
Let me know if I misinterpreted what we are trying to do?

I did get a little lost with some of the stripping so wasn't sure what is and is not needed, but as an example of a bash way:
Code:
echo ${CD_Dir// /}
The above will remove any spaces
 
Old 12-11-2015, 08:24 AM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by grail View Post
See if this helps at all, I did change some variable names just to make it a little clearer:
Code:
start_path='/home/userx/JunkMusic1/Cd 101/The Puppet - Echo & The Bunnymen.mp3'
path=${start_path/*}
ext=${start_path##*.}
match='^cd'

echo "4444444444444444444444444444"

if [[ "$ext" == 'mp3' ]] ; then

  CD_Dir=${path##*/}

  if [[ "${CD_Dir,,}" =~ $match ]] ; then

    echo "foundCD : $CD_Dir"

    nPath=${path%/*}

    AlbumDir=${nPath##*/}

    echo "AlbumDir : $AlbumDir"

  else
    echo "NO CD DIR : $CD_Dir"

  fi  
fi

echo "RRRRRRRRRRRRRRRRRRRRRRR"
Let me know if I misinterpreted what we are trying to do?

I did get a little lost with some of the stripping so wasn't sure what is and is not needed, but as an example of a bash way:
Code:
echo ${CD_Dir// /}
The above will remove any spaces
You're spot on in what I am trying to do.
Code:
path=${start_path/*}
ext=${start_path##*.} 
#then added
match2=^disk
you did forget to put the marker thingy % in this one though, and
Code:
path=${start_path/*}
should be like this
Code:
start_path=${FILENAME%/*}
though the logic is right , the path var has already stripped out the file with the ext on it. therefore the next bit of code did not work as a result of it. No Big Deal, really. I just removed that part of it. Had you started with the FILENAME then stripped the ext off of that instead, like this
Code:
while [ x -lt y ] ; 
do read FILENAME;

path=${FILENAME%/*} 
file=${FILENAME##*/}
ext=${file##*.}
# the ext is stripped to the farest right side starting off the left side just incase there is another . period within the file name.
# insuring you get the right period just before the ext.

done
with the code I am using to do that, strip it down giving path, filename, ext, I found it on the internet why they make it so long I do not know, I just keep copy pasting it,

no biggie. When I removed your path code keeping the rest I then got some output, but yeah! it's catching them now from what I see, because it flys by on the Term really fast, so I put a sleep in it when it finds a CD / disk dir. Thanks 4 ur Help!

I've got that
Code:
 var=$[var//_/ } # var//search pattern/replace/} and var${var#*pattern}
down pretty good now. But what is this doing that you did with this part of the code?

Code:
"${CD_Dir,,}"
the two ,, within the brackets. what does that do for it?
 
Old 12-11-2015, 12:44 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Yes the first % sign is always eaten for some reason ... I forgot to check.

Quote:
though the logic is right , the path var has already stripped out the file with the ext on it. therefore the next bit of code did not work as a result of it. No Big Deal, really. I just removed that part of it. Had you started with the FILENAME then stripped the ext off of that instead, like this
As I do not have any files with that name, my 'start_path' variable was equivalent to a found file hence 'FILENAME', would be the same as 'start_path'

Quote:
# the ext is stripped to the farest right side starting off the left side just incase there is another . period within the file name.
# insuring you get the right period just before the ext.
The above is incorrect as ## is greedy and will overlook all periods until it finds the last one, hence it will find the one prior to the extension

,, sets all in the variable to lower case
^^ is partner to change all to upper case
 
Old 12-11-2015, 01:37 PM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by grail View Post
Yes the first sign is always eaten for some reason ... I forgot to check.
Not arguing -- it is nothing to arguee about there -- it's kew
Quote:
Originally Posted by grail View Post
As I do not have any files with that name,
what ???? you got no mp3's ...
Quote:
Originally Posted by grail View Post
The above is incorrect as ## is greedy and will overlook all periods until it finds the last one, hence it will find the one prior to the extension
no, how so? it looks to the furthest . therefore it will only catch that last period, which will be the one just before the ext - try it, put 1 or more periods within a file then try it out.
Code:
ext=${file#*.}
and
ext=${file##*.}
then see what it gets you. here I did it for you.
Code:
find "$DIRNAME" -type f -name "*.*" | while [ $Count -ne  '10'  ] ;
		do read FILENAME;
		echo "*************"
	echo "FILENAME   $FILENAME"	
	path=${FILENAME%/*}  
	file=${FILENAME##*/}
	ext1=${file#*.}
	ext2=${file##*.}
		
	echo "path $path"
	echo "file $file"
	echo "ext1 $ext1"
	echo "ext2 $ext2"
	echo
	echo "***********"

let Count++

done
results: as you see the one # only gets the very first . period whereas you have to use two ## to get to the right end of the file name using this methiod .. it is not being greedy.
Code:
FILENAME   /home/userx/NewFIleSearch/TestFolder/Deja. . Voodoo . - Blues. Kenny. . . . . . . Wayne Shepherd Cd.mp3
path /home/userx/NewFIleSearch/TestFolder
file Deja. . Voodoo . - Blues. Kenny. . . . . . . Wayne Shepherd Cd.mp3
ext1  . Voodoo . - Blues. Kenny. . . . . . . Wayne Shepherd Cd.mp3
ext2 mp3

***********
or
Quote:
Originally Posted by grail View Post
The above is incorrect as ## is greedy and will overlook all periods until it finds the last one, hence it will find the one prior to the extension
NO because that is what we are looking for, the extention. It chops that period off and everything to the left of it, leaving just the extention. as you just seen in my actual data output showing you

Code:
# - goes left to right shortest
## - goes left to right to the furthest
% - goes right to left shortest
%% - goes right to left to the furthest
Quote:
Originally Posted by grail View Post
,, sets all in the variable to lower case
^^ is partner to change all to upper case
Thanks now I've learned another thing,

Last edited by BW-userx; 12-11-2015 at 02:25 PM.
 
  


Reply

Tags
bash scripting, egrep, strings



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
egrep to match all multiple pattern LYC Linux - Newbie 6 10-23-2015 08:13 PM
[SOLVED] BASH ZCAT EGREP Shell Script metallica1973 Programming 6 10-23-2012 02:12 PM
Unidentified error in bash script that notepad ++ is not catching tfarnsworth74 Linux - Newbie 4 07-10-2011 07:03 PM
awk: What pattern has the same meaning with egrep -w ? quanba Other *NIX 6 10-12-2009 12:22 AM
bash script, empty (enter button pushing only) value catching junust Programming 2 12-25-2008 04:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:07 AM.

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