LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-25-2012, 10:58 AM   #1
elalexluna83
Member
 
Registered: Mar 2012
Posts: 55

Rep: Reputation: Disabled
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
 
Old 04-25-2012, 11:19 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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?
 
Old 04-25-2012, 12:03 PM   #3
elalexluna83
Member
 
Registered: Mar 2012
Posts: 55

Original Poster
Rep: Reputation: Disabled
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
...
...
...
 
Old 04-25-2012, 12:16 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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?
 
Old 04-25-2012, 12:29 PM   #5
elalexluna83
Member
 
Registered: Mar 2012
Posts: 55

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-25-2012, 01:11 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 04-25-2012, 03:46 PM   #7
elalexluna83
Member
 
Registered: Mar 2012
Posts: 55

Original Poster
Rep: Reputation: Disabled
thanks a bunch, i had not seen your previous post.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash - rename multiple files with dots in filename eldude543 Programming 20 05-20-2016 11:17 PM
Help with bash script to rename multiple files zookman Programming 1 05-09-2011 03:23 AM
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
Rename multiple files in Bash damianpfister Programming 5 11-15-2009 08:30 AM
how to rename files using sed? seimour Linux - Newbie 1 05-02-2007 01:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration