sed cut characters of string

helloo

I wonder if there's a way to cut characters out of a string and keep only
the last 2 by using sed.

For example if there's the todays' date:

2012-05-06

and we only want to keep the last 2 characters which are the day.
Is there a quick way to do it with sed?

Yes, but exactly how depends on a few things. Is this string a part of a record contained in a file, and thus you want to replace all of these strings with just the last two chracters? Or, is this string in a ksh or bash script variable, and you'd like to truncate the string.

If it's the latter (you have the value in a script variable), there are better ways than using sed. For instance:

date="2012-12-01"
echo "${date##*-}"       # deletes all characters up to, and including the last dash

If you have a file, then the syntax of each record of the file needs to be known in order to craft a sed programme that will do the work.

1 Like

nice solution!
thanks a lot!:b: