sam sam - 3 months ago 19
Bash Question

How to remove 4 characters before a file extension using shell

I have a lot of Urls similar to this.

<random_tex>/original/Widget-Examples-FAQ3dad.png?1437083013

<random_tex>/original/Optimizec9cd.png?1413334050


I want to strip of the 4 characters before
.png
the output should be something like

<random_tex>/original/Widget-Examples-FAQ.png?1437083013
<random_tex>/original/Optimize.png?1413334050


I've tried using
sed
to replace but wasn't able to find the pattern for the exact 4 characters before
.png


sed
I've tried is :
sed 's/\(.{0,3}\.\)png/\1png/'

Answer

You can do, POSIX-ly:

sed 's/.\{4\}\(\.png\)/\1/'
  • .\{4\} matches 4 characters before .png, in the replacement just used .png

Note that, for multiple .png patterns, the first one will be modified.

Example:

$ sed 's/.\{4\}\(\.png\)/\1/' <<<'/original/Widget-Examples-FAQ3dad.png?1437083013'
/original/Widget-Examples-FAQ.png?1437083013

$ sed 's/.\{4\}\(\.png\)/\1/' <<<'/original/Optimizec9cd.png?1413334050'
/original/Optimize.png?1413334050
Comments