Use `sub` and `gsub` for replacements.
Section: awk Scripting Patterns
Replace text in awk
bash
bash
awk '{gsub(/foo/, "bar"); print}' file.txtExplanation
`sub` replaces one match, while `gsub` replaces all matches in the current record.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing awk Scripting Patterns
Use BEGIN and END blocks
Print headers and footers around processed output.
Print selected columns
Show the first and third whitespace-delimited fields.