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)

Community
  • 1
  • 1
aironman
  • 837
  • 5
  • 26
  • 55

2 Answers2

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!

Community
  • 1
  • 1
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 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". – chepner Nov 13 '14 at 13:49
  • And you noted the same thing in your answer while I was commenting :) – chepner Nov 13 '14 at 13:49
  • Actually I learned something new today, I had not stumbled across this particular twist in the syntax before. Thanks @Benoit wherever you are (-: – tripleee Nov 13 '14 at 13:51
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?

ua2b
  • 182
  • 3