remove only the last extension from file name

bash, edit, file, tcsh

Solution

check this if it works for your requirement:

sed

sed 's/\.[^.]*$//'

grep

grep -Po '.*(?=\.)'

test:

kent$  cat f
name_1.23.ps.png
name_1.23.ps.best
name_1.23.ps
name_1.23.ps

#sed:
kent$  sed 's/\.[^.]*$//' f
name_1.23.ps
name_1.23.ps
name_1.23
name_1.23

#grep
kent$  grep -Po '.*(?=\.)' f
name_1.23.ps
name_1.23.ps
name_1.23
name_1.23

EDIT from the comments. I feel it would be new requirement:

grep

kent$  grep -o '.*\.ps' f                                                                                         
name_1.23.ps
name_1.23.ps
name_1.23.ps
name_1.23.ps

sed

kent$  sed 's/\(.*\.ps\)\..*/\1/' f
name_1.23.ps
name_1.23.ps
name_1.23.ps
name_1.23.ps

Problem

I have file names that look something similar to this ``` name_1.23.ps.png ``` or ``` name_1.23.ps.best ``` or ``` name_1.23.ps ``` I want to take off the random file extensions on the end and be left with just ``` name_1.23.ps ``` Other questions similar to this use '.' as a delimator but this removes everything after name_1. I want to do this on the command line (in tcsh or bash)

Original source