0

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!

jamie
  • 99
  • 1
  • 1
  • 5

1 Answers1

2

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
Jotne
  • 40,548
  • 12
  • 51
  • 55