Operating System - Linux
1829749 Members
1816 Online
109992 Solutions
New Discussion

Re: delete 9 characters of filename (linux)

 
jaegere
New Member

delete 9 characters of filename (linux)

I would like to delete 9 characters of filenames of different lengths: I use tcsh!!!
wsgsmax200011.nc
va925_199011.nc
va925_200011.nc
wss200011.nc
echo $file | cut -c 1-4: cuts the first 4 char.
I guess sed would be the right tool to do so:
echo $file | sed -e 's/[1,2].*nc//g'
works fine except for va925_200011.nc:
va9 instead of va925_
I guess I need a regular expression for the last 9 char.

cheers
Eric
2 REPLIES 2
Matti_Kurkela
Honored Contributor

Re: delete 9 characters of filename (linux)

The last 9 characters?

You need to use $ to anchor the match to the end of the string.
The simplest way would be:

echo $file | sed -e 's/.........$//g'

A bit more specific variant would be:

echo $file | sed -e 's/......\.nc//g'
MK
jaegere
New Member

Re: delete 9 characters of filename (linux)

thanks alot

it works perfectly!!!