0

I'm trying to replace a guid without hyphens from some file names.

I have the regex done, I think, however I can't seem to get either the escaping correct or the replacement commands to work with each other.

Here's my regex

https://regex101.com/r/SiqsjP/1

(-[0-9a-f]{32})

With file names like this

iPhone6Plus-learn_multi_child-0dfb2dc71fe20da66ca47190d3136b12.png

I've seen this answer Bash shell script to locate and remove substring within a filename but it's not quite the same...

I think this should work, but it doesn't complain of an error ?

newname=`echo "$filename" | sed -e 's/\([0-9a-f]{32}\)\.png/\1.png/'`
1

Using string manipulations in the shell:

for name in *.png; do
    # remove everything after the last '-' including the dash
    # and add the '.png' extension back
    newname="${name%-*}.png"
    echo mv "$name" "$newname"
done

This assumes that all files that you'd like to rename are .png files in the current directory.

Run this once and remove the echo if it seems like it's doing the right thing.

3

In sed BRE (basic regular expressions) you should also escape curly braces {}:

newname=`echo "$filename" | sed 's/-[0-9a-f]\{32\}//g'`

To move/rename the file:

mv "$filename" "$newname"
  • Thanks, although again this doesn't produce an error, that doesn't rename the files – Jules May 21 '17 at 19:59
  • your command doesn't rename the file at all. It was a substitution. You should deal with mv command additionally – RomanPerekhrest May 21 '17 at 20:01
  • should that be piped also? – Jules May 21 '17 at 20:05
  • @Jules, no, line by line, see my update. – RomanPerekhrest May 21 '17 at 20:07
  • Do I need for around it? – Jules May 21 '17 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.