[sed] Replace the second occurrence on each line

Created on November 30, 2026

Put a number at the end of a substitution command to select the occurrence to replace. The 2 in s/foo/bar/2 replaces the second foo with bar.

printf 'foo foo foo\n' | sed 's/foo/bar/2'
foo bar foo
Bash Shell command sed