downsizing of strings which are returned by grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting downsizing of strings which are returned by grep
# 15  
Old 07-20-2009
yes that was the problem.
BUT, a new prob now.. the loop becomes weird...

now all the files are getting deleted and only one file remains, with the filename "-"

maybe because i am constantly changing the contents of the working directory, it seems everytime the name is changed, the loop thinks it is a new item added and finally, there is only one file left. This is probably a logical error.

In fact i had earlier tried to rename the files using python.. and exactly the same thing happend!! i don't know what the problem is but its definaltely a logical error.

heres the python code if u wnat to look a it:

Code:
import os

os.chdir(raw_input('plz enter directory name: '))
lst=os.listdir(os.getcwd())
class process:
	def getmetadata(self,filename):
		command='''mminfo %s|grep artist>/tmp/temp.txt'''%filename;#print command
		os.system(command)
		q=file('/tmp/temp.txt');self.artist=self.get(q.read());q.close()
		command='''mminfo %s|grep title>/tmp/temp.txt'''%filename
		os.system(command)
		q=file('/tmp/temp.txt');self.title=self.get(q.read());q.close()

	def __init__(self,filename):
		self.getmetadata(filename)
		os.rename(filename,self.artist+'_'+self.title+'.mp3')

	def get(self,s):#this returns the string after ':' in s
		num=s.find(':')
		return s[num+1:]
#print lst
for i in lst:
	#print i
	instance=process(str(i))	
	#print instance.title,instance.artist
#os.remove('/tmp/temp.txt')#/home/abhigyan/s



---------- Post updated at 01:44 PM ---------- Previous update was at 01:40 PM ----------

k man.. im beat.. thanks a lot ryan for everything, ill look into this later.. if you can, try to run the both the shell script as well as the python script on test file.. thanks again though..

funny thing is, the python script actually works if i run it twice!! but the prob is.. it deletes everything except for that last file.Smilie
# 16  
Old 07-20-2009
i'm not sure what the error you are getting try running in expanded mode

Code:
#!/bin/bash -x



---------- Post updated at 04:22 PM ---------- Previous update was at 04:18 PM ----------

no prob dude. you're d man!
# 17  
Old 07-22-2009
DONE!! Give me feedback ppl.. let me know how this works..

ok guys the prob was that i was changing the files in the folder itself so the loop was getting messed up.. i reworte it using a souce dir and a target dir and it worked like a charm..w00t

Code:
#!/bin/bash


#Renames and organizes your music data.
#Provide the full path of your source music folder as an argument.
#Renames music files according to its metadata  in format "Artist - Title". 
#Creates a directory in the target folder with Artist name and Album name and stores file in it.


if [ -z "$1" ];then #setting default directory
	cd /home/abhigyan/Desktop/source
else	
	cd "$1"
fi

target_dir='/home/abhigyan/Desktop/target'

for F in ./*.mp3 #change this line to 'for F in ./*' if you have files other than mp3 format
do
	if [ -f "$F" ];then				
		artist=`mminfo "$F"|grep artist|awk -F: '{print $2}'` #getting metadata
		title=`mminfo "$F"|grep title|awk -F: '{print $2}'`
		album=`mminfo "$F"|grep album|awk -F: '{print $2}'`
		if [ -z "$album" ];then
		album="Unkown Album"
		fi
			if [  -n "$artist"  ] && [  -n "$title"  ];then
				filename="$artist - $title.mp3";#echo $filename
				cp "$F" "$filename"  #renaming the file(copy)
					if [ -d "$target_dir/$artist/$album" ];then    #moving file to the artist folder
						mv "$filename" "$target_dir/$artist/$album"  
					else
						mkdir -p "$target_dir/$artist/$album"
						mv "$filename" "$target_dir/$artist/$album"
					fi
			else
				echo -e "Skipping b/c insufficient metadata: $F \n"
			fi
	fi
done


Last edited by abhigyan91; 07-22-2009 at 05:22 AM.. Reason: now it sorts into albums too...:)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep strings for different cases

Hi All, Good morning I have a below code which is working & getting expected output. the problem in this code is it is executing 3 if conditions, my requirement is suppose if first condition is success then it should print echo statement & exit from if condition else if the 1st if condition... (4 Replies)
Discussion started by: sam@sam
4 Replies

2. Shell Programming and Scripting

Grep for strings

Hi, Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36 How can I grep for the strings chrome and safari from a file, and if chrome print Chrome/40.0.2214.94 to a file and also count the number of times chrome is found? ... (4 Replies)
Discussion started by: cyberfrog
4 Replies

3. Shell Programming and Scripting

GREP between last occurrences of two strings

Hi I have the server.log file as: Server Stopped ABC DEF GHI JKL Server Started MNO PQR STU Server Stopped VWX YZ ABC Server Started Server Stopped 123 456 789 (9 Replies)
Discussion started by: ankur328
9 Replies

4. Red Hat

Grep between two strings in shell

<cisco:subname> <cisco:sptp>Cisco PortA Series</cisco:sptp> <cisco:aliasNameList xsi:nil="true"/> <cisco: owner xsi:nil="true"/> <cisco:subportname> <cisco:cpt>Cisco SubPort B Series</cisco:cpt> ... (3 Replies)
Discussion started by: itsspy
3 Replies

5. Shell Programming and Scripting

Grep 2 same strings in a same line??

I have this code TrackingId:1362412470675;MSISDN:; INFO - number of clietns:3:Received response is: EMSResponse , protocolVersion=5, purchaseOptions=null, serviceData=ServiceData , screenData=CanvasData ]], title=null, titleResource=MessageResource], screenType=null]], serviceId=idBamboo,... (7 Replies)
Discussion started by: nikhil jain
7 Replies

6. Shell Programming and Scripting

Can't grep multiple strings

I have a script that periodically checks the Apache error_log to search for a specific error that causes it to hand and, if found, it restarts the service. I recently found another error that forces it to hand and won't serve pages until it is reset. What I'm trying to do is to get the script to... (3 Replies)
Discussion started by: cfjohnsn
3 Replies

7. Shell Programming and Scripting

Grep Multiple Strings

Hi, Can any one pelase tell me how to grep multiple strings from multiple files in a singel folder? grep -E "string1|string2|string3|string4|string..." its taking lots of time.. can any please tell me fast grep??? URGENT (10 Replies)
Discussion started by: durgaprasad
10 Replies

8. Shell Programming and Scripting

want to grep only strings in a file?

Hai, Just want to print only alphanumeric in a file ex:- fdsdsklf#@^%$#hf output:- fdsdsklfhf plz, help me:o (5 Replies)
Discussion started by: balan_mca
5 Replies

9. Shell Programming and Scripting

number of lines returned from a grep command

hi all, from a shell (ksh) script, i am doing a 'grep'. how do i find out the number of lines returned from that 'grep' command ?? thanks in advance. (4 Replies)
Discussion started by: cesarNZ
4 Replies

10. UNIX for Dummies Questions & Answers

Copying file names returned from a grep command into another directory

When I do the following : grep -l "string" *, I get a list of file names returned. Is there a way to copy the files returned from the list into another directory ?. Thanks. (4 Replies)
Discussion started by: Kartheg
4 Replies
Login or Register to Ask a Question