Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

sed to rename files in bash loop

Shell Programming and Scripting


Tags
bash, sed, solved

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-20-2017
cmccabe cmccabe is offline
Registered User
 
Join Date: Nov 2013
Last Activity: 23 June 2017, 9:42 AM EDT
Location: Chicago
Posts: 1,141
Thanks: 681
Thanked 14 Times in 13 Posts
sed to rename files in bash loop

I am trying to use sed to rename all .txt files in /home/cmccabe/test. However, I am getting an error that I seems to be putting the files in a new directory s, instead of in the original. Thank you Linux.

bash

Code:
# rename classified
cd /home/cmccabe/test
pattern2_old="_classify"
pattern2_new="hg19multianno"

for file in *.txt
do
        newname=`printf '%s\n' "$file" | /bin/sed -e "s/$pattern2_old/$pattern2_new//"`
        /bin/mv -fv "$file" "$newname"
done
/bin/sed: -e expression #1, char 27: unknown option to `s'
‘17-0000_classify.txt’ -> ‘’
/bin/mv: cannot move ‘17-0000_classify.txt’ to ‘’: No such file or directory
/bin/sed: -e expression #1, char 27: unknown option to `s'
‘17-0001_classify.txt’ -> ‘’
/bin/mv: cannot move ‘17-0001_classify.txt’ to ‘’: No such file or directory


contents of /home/cmccabe/testbefore script executed


Code:
17-0000_classify.txt
17-0001_classify.txt
17-0002_classify.txt

contents of /home/cmccabe/testafter script executed (desired output)


Code:
17-0000_hg19multiannotxt
17-0001_hg19multianno.txt
17-0002_hg19multianno.txt


Last edited by rbatte1; 03-21-2017 at 08:50 AM.. Reason: Moved a closing CODE tag to make more sense
Sponsored Links
    #2  
Old Unix and Linux 03-20-2017
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 22 June 2017, 5:05 AM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,446
Thanks: 100
Thanked 1,509 Times in 1,119 Posts
Quote:
Originally Posted by cmccabe View Post
However, I am getting an error that I seems to be putting the files in a new directory s, instead of in the original. Thank you Linux.
You get the error because this:

Code:
sed -e "s/$pattern2_old/$pattern2_new//"

is not a valid sed-command. The s-subcommand has the form:


Code:
s/<regexp>/<regexp>/

and the last slash at the end leads to a simple syntax error.

Furthermore, your script is about as badly written as can be:


Code:
cd /home/cmccabe/test

Never do that in a script. Ideally you should use a commandline argument to provide the directory in question, but in absence of this do it this way:


Code:
dir="/home/cmccabe/test"

for file in "$dir"/* ; do
    mv $file ....
done

Secondly:

Code:
for file in *.txt
do
        newname=`printf '%s\n' "$file" | /bin/sed -e "s/$pattern2_old/$pattern2_new//"`
        /bin/mv -fv "$file" "$newname"
done

I spare you the obligatory sermon about not using backticks: you have been told this perhaps several dozen times already and i find the insistence to write bad scripts commendable. The POSIX variant of $(...) has only been developed to have something one can safely ignore.

You still may consider to invoke sed only once per run of the script instead of once per file the script encounters:


Code:
old="classify"
new="hg19multianno"

ls -1 "$dir"/*.txt |\
sed 's/\(.*\)'"$old"'\(.*\)/\1'"$old"'\2 \1'"$new"'\2/' |\
while read oldfile newfile ; do
     mv "$oldfile" "$newfile"
done

But even this is just a waste of processing power and other resources, because you can do the same with variable expansion inside the shell, without any external program:


Code:
old="classify"
new="hg19multianno"

ls -1 "$dir"/*txt |\
while read file ; do
     mv "$file" "${file/${old}/${new}}"
done

I hope this helps.

bakunin
The Following User Says Thank You to bakunin For This Useful Post:
cmccabe (03-25-2017)
Sponsored Links
    #3  
Old Unix and Linux 03-25-2017
cmccabe cmccabe is offline
Registered User
 
Join Date: Nov 2013
Last Activity: 23 June 2017, 9:42 AM EDT
Location: Chicago
Posts: 1,141
Thanks: 681
Thanked 14 Times in 13 Posts
Thank you very much for your help and explanations Linux.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bash script to rename files in a directory djsnifer UNIX for Dummies Questions & Answers 4 02-24-2012 04:08 PM
rename files using loop with different name feroz Linux 3 06-27-2010 11:57 PM
Rename files with sed in an until loop (double post) jvpike UNIX for Dummies Questions & Answers 0 03-06-2010 03:24 PM
Rename files without using for loop Qwerty123 UNIX Desktop Questions & Answers 3 12-07-2009 10:56 AM
Hello - new here - bash script - need to rename and zip files. Aixia Shell Programming and Scripting 1 07-15-2008 02:48 PM



All times are GMT -4. The time now is 06:31 AM.