0

i have this script and a dir file with some Sip_* files and i need to rename them to SIP_*

for i in Sip_R*.csv
do
   echo "processing $i"
   NEWNAME="$(i/Sip%/SIP)"
   echo "newname is $NEWNAME"
   mv -- "$i" "$NEWNAME"
done

and i receiving

 processing Sip_Reg-generated1415879496958.csv
 digest-reg-files.sh: line 5: i/Sip%/SIP: No such file or directory
 newname is
 mv: cannot move `Sip_Reg-generated1415879496958.csv' to `': No such file or directory
 processing Sip_Reg-generated1415879504694.csv
 digest-reg-files.sh: line 5: i/Sip%/SIP: No such file or directory
 newname is
 mv: cannot move `Sip_Reg-generated1415879504694.csv' to `': No such file or directory

I have modified the solution provided by Renaming multiples files with a bash loop and it is not working.

The machine is some centos, 2.6.32-358.el6.x86_64 and bash version is 4.1.2(1)-release (x86_64-redhat-linux-gnu)

2
  • Have you tried using rename instead? $ rename Sip_ SIP_ *.csv linux.die.net/man/1/rename Commented Nov 13, 2014 at 13:02
  • 1
    NEWNAME=${i/Sip/SIP}. Commented Nov 13, 2014 at 13:15

2 Answers 2

2

Your brackets are wacky. The syntax for string substitution in Bash uses curly braces, not round parentheses.

Also, like the answer you link to explains, the percent sign anchors the substitution to the end of the string, which is not what you want here. Use /# instead to anchor to the beginning of the string; or you could use ${i/Sip/SIP} to not anchor at all (you already know the first occurrence is at the beginning of the string anyway).

NEWNAME="${i/#Sip/SIP}"

Kudos for properly quoting everything!

Sign up to request clarification or add additional context in comments.

3 Comments

The percent sign in the linked answer is the suffix-anchor modifier to ${var/}: "${i/%.fasta/.fa}" means that .fasta is only replaced by .fa if it occurs at the end of the parameter value. As a sed-like substitution, it would look like s/\.fasta$/.fa/. The OP might have mistaken it for * to match any preceding characters; its use here would seem to attempt to match everything following "Sip".
And you noted the same thing in your answer while I was commenting :)
Actually I learned something new today, I had not stumbled across this particular twist in the syntax before. Thanks @Benoit wherever you are (-:
1

The original solution mentioned above uses something like (in your case):

NEWNAME="$( echo "$i" | sed -e 's/Sip/SIP/' )"

The content of $(…) is executed in a subshell and must be regular code which i/Sip%/SIP is not. Maybe a typo?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.