mardi 20 septembre 2016

awk condition on whether pattern is matched

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?

Thanks

Aucun commentaire:

Enregistrer un commentaire