09-06-2023, 11:13 AM
How to Replace All the Occurrences of the Pattern in a Line Using the sed Command
By default, sed only substitutes the first occurrence of a specified string in every line. It looks for the initial instance, replaces it, and moves on to the following input line.
To replace all the pattern occurrences in one line, add a substitute flag /g for global replacement. Here’s the sed script:
sed ‘s/old_string/new_string/g’ samplefile.txt
For example, to replace all occurrences that contain “eagle” with “falcon” in a line inside animals.txt, run:
sed ‘s/eagle/falcon/g’ animals.txt

