System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

Sed display only last 8 character of filename.

 
Ben Alcock
Occasional Visitor

Sed display only last 8 character of filename.

I would like to display the last 8 characters of the filenames for filenames of different lengths.

I can delete the last 8 characters with sed but dont know how to only show the last 8 characters.

The filenames are something like;

afxH340800340000
afxH30800340021
afxR3080034002122

I have used;

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

Any help is greatly apprecaited.

Cheers

Ben
3 REPLIES
Ivan Ferreira
Honored Contributor

Re: Sed display only last 8 character of filename.

You could use:

awk '{ print substr($0,length($0)-8,length($0)) }' file
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Matti_Kurkela
Honored Contributor

Re: Sed display only last 8 character of filename.

Here's how to solve it with sed:

echo $file | sed -e 's/^.*\(........\)$/\1/g'

MK
MK
Ben Alcock
Occasional Visitor

Re: Sed display only last 8 character of filename.

Thanks guys, I ended up solving this with

echo "afxH340800340000" | sed -e 's/.*\(.\{8\}\)$/\1/'