~:: kalyan ::~

November 2, 2008

AWK After Diwali

Filed under: General, Script — Tags: — skalyanasundaram @ 6:55 am

Took a long break for Diwali, six days stay at home. After Diwali settled down at home had nothing to do other than sleeping and eating. TV was boaring. So sat with awk manual. I have had problem in managing my music library. The provider always add his name to the song name like “Sitename – Songname.mp3″. The meta data like album,composer can be managed all together with iTunes, but the name has to be renamed one by one manually. I was struggling to write a shell script. I knew there is some way with awk. And finally i found a way with awk script. Here it is.

ls -l | grep -v "total*" | awk '{val=($(NF-2)" "$(NF-1)" "$(NF)); printf("mv "%s" "%s" n",val, $NF)|"sh"} END{close("sh")}'

I have started using mutt, ppl are moving from text based application to desktop application and now to browser application. Johnny says i am going in the opposite direction. True! anyway this is just fun. I was configuring the addressbook for mutt. Though there are multiple options like lbdb, abook, I still wanted to use the simple mutt alias. But the command add-alias is interactive. I wanted to write a macro which just takes the from field and add to the alias without any interaction. Here is the macro which i wrote

macro index,pager <Esc>a "<enter-command>unset wait_key<enter><pipe-message> awk '{if($1=="From:") { sub(/^"/, "", $2); sub(/"$/, "", $(NF-1)); name=tolower($2) tolower($(NF-1)); print "alias", name, name, $NF >> "/home/tree/.mutt/Mutt-alias" }}' <enter> <shell-escape> awk '{lst[$2] = $0} END{ for(x in lst) print lst[x]  >> "/home/tree/.mutt/Mutt-alias.temp" ;}' /home/tree/.mutt/Mutt-alias;  mv /home/tree/.mutt/Mutt-alias{.temp,} <enter> <enter-command>set wait_key<enter> <enter-command>source /home/tree/.mutt/Mutt-alias<enter>" "Mutt Add address to alias file with no intraction"

This macro takes the first name and last name (middle name is ignored) and strips the any double quote at the beginning and in the ending and add the alias to the alias file and also the alias will be available immediatly. The problem was duplicate entries. The second awk script removes the duplicate entry. It just works fine for me. awk is cool.

update: The above script did not work when song name contain spaces too. So the new one looks like this,

ls -l Sitename* | grep -v "total*" | awk -F "Sitename.Com - " '{val=("Sitename.Com - "$(NF)); printf("mv "%s" "%s" n",val, $NF)|"sh"} END{close("sh")}'

The “sitename.com – “is the string i wanted to strip.

About these ads

1 Comment »

  1. Wow! Nice Post I like this MV Thanks for sharing and Happy New Year 2011 Please Visit My Music song site.

    Comment by เพลงใหม่ — January 26, 2011 @ 12:24 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Silver is the New Black Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: