Reputation: 517
I am trying to have awk alter a given pattern if matched or return the original line. Here's my code
printf 'hello,"hru, bro"\nhi,bye\n' | gawk 'match($0, /"([^"]+)"/, m) {if (m[1] == "") {print $0} else {print gsub(/,/,"",m[1])}}'
-> 1
I expect `match to return the matched pattern in m[1] and gsub to substitute all ',' in m[1] when there is a match. Thus the result should be
-> hello,hru bro\nhi,bye
What am I missing here?
UPDATE
According to Tom comment I replace gsub
with gensub
, yet I now get the following result:
-> gawk: cmd. line:1: (FILENAME=- FNR=1) warning: gensub: third argument `hru, bro' treated as 1
hello"hru, bro"
Upvotes: 0
Views: 793
Reputation: 74595
gsub
mutates the third argument and returns the number of substitutions made - in this case, 1
.
I would suggest changing your code to something like this:
awk 'match($0, /([^"]*")([^"]+)(".*)/, m) {
$0 = m[1] gensub(/,/, "", "g", m[2]) m[3]
} 1'
If there is anything surrounded by quotes on the line, then rebuild it, using gensub
to remove the commas from the middle captured group (i.e. the part between the double quotes).
Note that gensub
takes 4 arguments, where the third is used to specify the number of replacements to be made ("g"
means global).
Upvotes: 1