LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH - Rename multiple files using tr and sed (https://www.linuxquestions.org/questions/programming-9/bash-rename-multiple-files-using-tr-and-sed-941686/)

elalexluna83 04-25-2012 10:58 AM

BASH - Rename multiple files using tr and sed
 
Hi All!

I'd like to rename several files but the script gets hung,

any ideas

Code:

#!/bin/bash
ls -l | grep -v rename.sh | grep -v files | awk '{print $9}' > files.txt
tr [:upper:] [:lower:] | tr [:blank:] '_' | tr '.' '_' | tr '-' '_' | tr -s '_' | sed 's/^[0-9]*//' | sed 's/^\_//' | sed 's/\_DOC/\.doc/' < files.txt

files.txt
ONE_FILE-_-_(2008)_balance.doc
TWO_filR-_-_(2010)_balance.doc
THREE_File-_-_(2004)_balance.doc
FOUR_fiLe-_-_(2005)_balance.doc
FIVE_fIle-_-_(2005)_balance.doc

grail 04-25-2012 11:19 AM

hmmm ... maybe you could show us what you start with and the desired end result, as obviously the current solution is not doing what you want?

Also as there is no mv command anywhere, I am a bit perplexed at how any renaming is to occur?

elalexluna83 04-25-2012 12:03 PM

SOrry, that was an old version

Code:

#!/bin/sh
ls -l | grep -v rename.sh | grep -v files | awk '{print $9}' > files
while read line
do
new=`tr [:upper:] [:lower:] | tr [:blank:] '_' | tr '.' '_' | tr '-' '_' | tr -s '_' | sed 's/^[0-9]*//' | sed 's/^\_//' | sed 's/\_doc/\.doc/' < $line`
mv $line $new
done < files
exit 0

./rename.sh: line 8: $line: ambiguous redirect
mv: missing file operand
Try `mv --help' for more information.


expected output of the files

one_file_(2008)_balance.doc
two_filr-_-_(2008)_balance.doc
three_file-_-_(2004)_balance.doc
...
...
...

grail 04-25-2012 12:16 PM

Well firstly you should this about parsing ls and its dangers.

Then you will need to read this one on word splitting as you have not quoted your variables and have already shown with the code that the
file names will have spaces in them.

I presume you just want to play with tr and sed for practice? Because you would know that sed can handle all the changes required?

elalexluna83 04-25-2012 12:29 PM

finally i achived this

Code:

#!/bin/sh
ls -l | grep -v rename.sh | grep -v files | awk '{print $9}' | sed '1d' > files
while read line
do
new=`echo $line | tr [:upper:] [:lower:] | tr "'" "_" |tr [:blank:] '_' | tr '.' '_' | tr '-' '_' | tr -s '_' | sed 's/^[0-9]*//' | sed 's/^\_//' | sed 's/\_mp3/\.mp3/'`
mv $line $new
done < files
exit 0

Thanks.

grail 04-25-2012 01:11 PM

Well I see you ignored all the advice, so I will show you an alternative just in case:
Code:

#!/bin/bash

for file in *
do
    [[ ! -f "$file" || "$file" == "rename.sh" || "$file" == "files" ]] && continue

    new=${file,,}
    new=${new//[\' .-]/_}

    new=$(echo "$new" | sed -e 's/_+/_/g' -e 's/^[0-9]*_?//')

    mv "$file" "${new%_mp3}.mp3"
done


elalexluna83 04-25-2012 03:46 PM

thanks a bunch, i had not seen your previous post.


All times are GMT -5. The time now is 05:02 PM.