Wednesday, 10 September 2014

String Processing in BASH: Find and Replace


The Linux bash shell has many wonderful string processing techniques which if efficiently exploited, give you enormous power to manage your files. Though many techniques are available, I found the  find-replace technique the most interesting. 

Consider a real-life problem:

I have 50 mp3 files in a directory named “Tracks”. Now I want the blank space in each file name to be replaced by the  “-” symbol [ I want this because if you are working in command-line, typing file names with a blank space in them is rather tedious]. Now I have the GUI approach in which I will have to individually rename each of the 322 files one by one ! [ Oh God! Please take me away before I try this! ]. The other way is the command line, which can do your job in less than 10 seconds depending on your typing speed of course!

I assume that you are a bit familiar with the basics of  bash shell scripting, like the for loop, echo command, variables, wild card * and the $ character.

The basic format is :

${string/find/replace}

where string is the variable containing the string to be searched, find is the pattern to be found and replace is the string to be replaced. This will search the string for the given pattern and replace the first occurrence with the replacement string.
If you want to replace all the occurrences, use a '/’ before your pattern :

${string//find/replace}

Open your terminal and type in the following:

string="HelloWorld”
str=${string/l/+}
str2=${string//l/+}
echo $str
echo $str2

The output is self explanatory.



Now as we are familiar with the command, we can use it for renaming our music files. The basic idea is to use a for loop for the purpose.

IMPORTANT:
Before trying out a newly written script which does file management, it is highly recommended that you backup the directory you want to use your script on, because if your script contains errors, you may mess up with your files., even losing them forever.

Take a look at my files first:



Now here is the simple script:
For those who are not familiar with scripts, enter each line in the terminal, one by one.
First, make sure that you are in the right directory by using the cd command.

for i in *; do
     new=${i// /-}
     mv "$i" "$new"
done

Explanation:
The explanation is rather simple. The for loop uses a variable i for iteration, the iteration working on all files in the directory as we have used the '*' wildcard. In each iteration, i contains the file name. A variable, new, stores the processed new file name, containing the blank spaces replaced by “-”. Finally we rename the file name using the mv command.




Viola! All of my file names have been processed, the way I wanted, and it took me a very little amount of time. Isn't it great ???

There are many more such examples where you will come to know the real power of the command line. I hope this post was interesting for you guys. Don’t forget to comment your queries and suggestions below.

No comments:

Post a Comment