sam sam - 1 year ago 100
Bash Question

How to remove 4 characters before a file extension using shell

I have a lot of Urls similar to this.



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


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

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

Answer Source

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.


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

$ sed 's/.\{4\}\(\.png\)/\1/' <<<'/original/Optimizec9cd.png?1413334050'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download