Skip to main content

Awk copy digits to end of string [Resolved]

At the moment I'm using

awk -F':' '{ j=$0; gsub(/[^0-9]+/,"",j); print $0 j }'

Which will replicate all digits within the string and paste them to the end of the string, example -

input -

1Hello123

output -

1Hello1231123

However I only wish to replicated the numbers at the end of the string, not the start nor inbetween

So expected output in this scenario would be -

1Hello123123

In this output the 1 at the start hasn't been replicated.

Also must be done with AWK, not sed.


Question Credit: user363409
Question Reference
Asked July 21, 2019
Tags: awk
Posted Under: Unix Linux
19 views
1 Answers

A couple of options:

$ echo 1Hello123 | awk '{sub(/[0-9]+$/,"&&",$0)} 1'
1Hello123123

or

$ echo 1Hello123 | awk 'match($0,/[0-9]+$/) {print $0 substr($0,RSTART,RLENGTH)}'
1Hello123123

credit: steeldriver
Answered July 21, 2019
Your Answer
D:\Adnan\Candoerz\CandoProject\vQA