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
# 1  
Old 07-20-2009
downsizing of strings which are returned by grep

hey every1, i am a very new shell programmer. what i am trying to do is to rename a music file using its metadata(using mminfo)
the problem is, mminfo's output is very weird:
Quote:
abhigyan@abhigyan-laptop:~$ mminfo ~/Desktop/It\'s\ Not\ My\ Time.mp3
/home/abhigyan/Desktop/It's Not My Time.mp3
| title: It's Not My Time
| language: Undetermined
| langcode: und
| media: MEDIA_AUDIO
| artist: 3 Doors Down
| mime: audio/mpeg
| samplerate: 32000
| length: 243.173877551
| codec: MPEG Layer 3
| bitrate: 128
| fourcc: 0x55
| userdate: 2008
| album: 3 Doors Down
| genre: Rock
| mode: stereo
my final filename should be "$artist - $title"

however, when i use
Code:
artist=` mminfo ~/Desktop/It\'s\ Not\ My\ Time.mp3|grep artist`

echo $artist gives:
Quote:
| artist: 3 Doors Down
now i need to get rid of the " | artist: " part so i only have "3 Doors Down".
How do i do that? I tried reading man page for cut, but cudnt understand muchSmilie

sorry for such a long post for a relatively simple problemSmilie
# 2  
Old 07-20-2009
try
Code:
artist=` mminfo ~/Desktop/It\'s\ Not\ My\ Time.mp3|grep artist | awk -F: '{print $2}'`

# 3  
Old 07-20-2009
Yea.. it works thanks!!
Can you explain what that code does?
will it work for all song files assuming the appropriate meta-data is available?
# 4  
Old 07-20-2009
yes, i presume it will work. There are many ways to do this actually.

Code:
awk -F: '{print $2}'

The -F is the field separator specified with colon (: ) it prints the column 2 which is defined by print $2.

print $1 will print "| artist"
# 5  
Old 07-20-2009
this is what i actually set out to do:

plz have a look at the code below.. Something is rong with line 17Smilie

Code:
#!/bin/bash


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


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

for F in ./*
    if [ -f $F ];then
        artist=`mminfo $F|grep artist|awk -F: '{print $2}'` #getting metadata
        chkartist=$? 
        title=`mminfo $F|grep title|awk -F: '{print $2}'`
        chktitle=$?
            if [ $chkartist -eq 0  ||  $chktitle -eq 0 ];then
                filename="$artist - $title"
                mv $F $filename
                    if [ -d $artist ];then    #moving file to the artist folder
                        mv $filename ./$artist  
                    else
                        mkdir $artist
                        mv $filename ./$artist
                    fi
            else
            echo -e "Skipping b/c insufficient metadata: $F \n"
            fi
    fi
done

Quote:
abhigyan@abhigyan-laptop:~$ bash meta.sh
meta.sh: line 17: syntax error near unexpected token `if'
meta.sh: line 17: ` if [ -f $F ];then'

Last edited by abhigyan91; 07-20-2009 at 04:14 AM.. Reason: i added what the shell returns on executing the script;also bold changes in script
# 6  
Old 07-20-2009
seems good.. can you the post the error?
# 7  
Old 07-20-2009
edited:

abhigyan@abhigyan-laptop:~$ bash meta.sh
meta.sh: line 17: syntax error near unexpected token `if'
meta.sh: line 17: ` if [ -f $F ];then'
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