How to convert a column to last five digits

Hi

Suppose I have a input.csv file like this

Fort, 2034440-3333
Honda, b293489289
Toyota, 23423000

How to convert the second column to the following format, there are only two cases:
a.if the last five character is a hyphen plus four numeric digit, keep the the last five digits before the hypen as well as everything after hyphen (including hyphen).
b. if the last five character doesn't contain a hyphen, keep the last five digits.

so the output.csv is:

Fort, 34440-3333
Honda, 89289
Toyota, 23000

Thanks, I guess this will be done in awk but I have no idea how to do it.

$ 
$ cat f1
Fort, 2034440-3333
Honda, b293489289
Toyota, 23423000
$ 
$ perl -lne '/^(.*?, )(.*?)((\d|-)\d{4})$/ && print $1,substr($3,0,1) eq "-" ? substr($2,-5):"",$3' f1
Fort, 34440-3333
Honda, 89289
Toyota, 23000
$ 
$ 

tyler_durden

 
sed 's/[[:alnum:]]*\([0-9]\{5\}\)$/\1/; s/[[:alnum:]]*\([0-9]\{5\}-[0-9]\{4\}\)$/\1/' myfile

Or awk , just shorter :wink:

awk '{$2=($2~/-/)?substr($2,length($2)-9):substr($2,length($2)-4)}1' file

wow dan, that's amazing, would you mind explain a bit on how this works?
what is the ? symbol do?
what is the ($2~/-/) do?
what is the }1 in the end do?

Another one:

perl -pe's/[^,]*(\d{5}(-\d+)?)$/$1/' infile

Or:

sed  's/[^,]*\([0-9]\{5\}\(-[0-9]*\)\{,1\}\)$/\1/' infile 

It likes to use "if else" in awk, but shorter.

awk '{if ($2~/-/) {print $1 substr($2,length($2)-9)} else {print $1 substr($2,length($2)-4)}}' urfile