Question

I am trying to replace a string of numbers in the first column using awk, gsub and backreference.

For instance, my input file is

1-00001 1 1-00001
1-00001-01 1 1-00001
1-00001-02 1 1-00001

and my desired output is

1-00001-00 1 1-00001
1-00001-01 1 1-00001
1-00001-02 1 1-00001

I tried the following unix command

 awk '{gsub("^1-\([0-9]\)\([0-9]\)\([0-9]\)\([0-9]\)\([0-9]\)$","^1-\1\2\3\4\5-00$",$1); print}' input

and the output was

^1-^A^B^C^D^E-00$ 1 1-00001
1-00001-01 1 1-00001
1-00001-02 1 1-00001

Can anybody tell me what is wrong in my command? Thank you in advance!

Was it helpful?

Solution

I think you need to use gensubfrom gnu awk to get backreference. But if you do tell us what you want, input and ouput, it can be done differently. Like this:

awk 'split($1,a,"-")!=3 {$1=$1 "-00"}1' file
1-00001-00 1 1-00001
1-00001-01 1 1-00001
1-00001-02 1 1-00001
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top