Awk has two built-in functions to deal with string substitutions: sub()
and gsub()
. There are others built-ins functions for matching and extracting parts of substrings, but these are for substitutions. The main different between both is that sub()
will only do the substitution one time on the first instance of the match, and gsub()
will do it for every instance of the match; the g in front tries to detonate this distinction and stand for global as in all or the whole.
The syntax could be expressed as gsub(regex, replacement, string), where regex is an Extended Regular Expression to match, replacement is the substitution part and string is the sequence of characters you want the work to be done on.
Using the example given by RavinderSingh13:
awk '{gsub(/\,/," ",$0);print}'
In gsub()
, now we know:
/\,/
is the regex representing the comma we want to find
" "
is what we want to substitute for
$0
is the range of characters we want to work on, in this case the whole record.
After the function is done:
print
is for after the substitutions are done, display the whole modified record.
However, awk users are attracted by the terse and brief statement abilities that the language has to offer and how much does with so little user code, thus they thrive in applying the most idiomatic and succinct expressions possible. Don't be surprise if you see instead of:
awk '{gsub(/\,/," ",$0);print}' file.here
this:
awk 'gsub(/,/, " ")' file.here
or even
awk 'gsub(/,/, OFS)' file.here
The original command and these two latest produce the same output, in this case.
Or instead of:
awk -F"," 'BEGIN{OFS=" ";} {$1=$1; print}' file
this:
awk -F, '$1=$1' file