[awk] Replace all matches on a line with gsub

Created on November 30, 2026

Use gsub to replace all matches on the current line. Suppose fruits.txt contains the following lines.

apple pie
apple juice

This command replaces each apple with orange and prints the resulting line.

awk '{gsub(/apple/, "orange"); print}' fruits.txt
orange pie
orange juice
awk Shell command