Monday, February 23, 2009

Changing Internal Field Separator in a Bash Shell

It took me a long long time to change the IFS (Internal Field Separator) in a bash shell. Which by the way is set as default to whitespace, tab, linefeed and carriage return.

If you want to change it in the shell you have to enter: IFS="whatever you want"
You can view the same using : echo $IFS | cat -vte

My problem was to rename files with spaces in them. As for loop was breaking each file name at space.

So I had to do the following:
# Before you modify your IFS please please please make sure you save your old IFS just in case things do not go as expected. Remember Murphy's Laws are omnipresent!

#IFS=:Space:Tab:Line Feed:Carriage Return:

Then rename each file.

for f in `ls *.txt`; do
echo $f
# change each file extension from .txt to .csv
mv $f `basename $f .txt`.csv
#Additionally if you want to remove the whitespace too then the following line replaces it with underscore!
# mv $f `basename $f .txt|sed 's/ /_/g'`.csv

And done in seconds..
sigh (of relief and happiness)!!!

Happy bashing! :)

No comments: