Operating System - Linux
1753342 Members
5159 Online
108792 Solutions
New Discussion юеВ

Re: Sed display only last 8 character of filename.

 
Ben Alcock
New Member

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 3
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
New Member

Re: Sed display only last 8 character of filename.

Thanks guys, I ended up solving this with

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