Skip to main content
simplified, ; needed to separate statement in POSIX awk. [0-9] is unspecified in POSIX outside the C locale.
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k

With the match() function of GNU awk to store the matching group in an array, you could do

awk -F: ' BEGIN { OFS = FS-v }OFS=: match'match($1, /([0-9]+)$/ , arr ) { $2 = $2 "" arr[1] } 1' file 

On any POSIX complaintcompliant awk you could do

awk -v FS=F: ' BEGIN { OFS = FS-v }OFS=: match'match($1, /([0-9]+)$[[:digit:]]+$/ ) { $2 = $2 "" substr($0, RSTART, RLENGTH) }; 1' file 

See How to permanently change a file using awk? ("in-place" edits, as with "sed -i") to make the file change persistent.

With the match() function of GNU awk to store the matching group in an array, you could do

awk -F: ' BEGIN { OFS = FS } match($1, /([0-9]+)$/ , arr ) { $2 = $2 "" arr[1] } 1' file 

On any POSIX complaint awk you could do

awk -v FS=: ' BEGIN { OFS = FS } match($1, /([0-9]+)$/ ) { $2 = $2 "" substr($0, RSTART, RLENGTH) } 1' file 

See How to permanently change a file using awk? ("in-place" edits, as with "sed -i") to make the file change persistent.

With the match() function of GNU awk to store the matching group in an array, you could do

awk -F: -v OFS=: 'match($1, /([0-9]+)$/ , arr) { $2 = $2 arr[1] } 1' file 

On any POSIX compliant awk you could do

awk -F: -v OFS=: 'match($1, /[[:digit:]]+$/) { $2 = $2 substr($0, RSTART, RLENGTH) }; 1' file 

See How to permanently change a file using awk? ("in-place" edits, as with "sed -i") to make the file change persistent.

Source Link
Inian
  • 13.1k
  • 2
  • 42
  • 56

With the match() function of GNU awk to store the matching group in an array, you could do

awk -F: ' BEGIN { OFS = FS } match($1, /([0-9]+)$/ , arr ) { $2 = $2 "" arr[1] } 1' file 

On any POSIX complaint awk you could do

awk -v FS=: ' BEGIN { OFS = FS } match($1, /([0-9]+)$/ ) { $2 = $2 "" substr($0, RSTART, RLENGTH) } 1' file 

See How to permanently change a file using awk? ("in-place" edits, as with "sed -i") to make the file change persistent.