문제

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!

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top