[sed] Reorder captured groups in a replacement

Created on November 30, 2026

Use backreferences such as \1 and \2 to include captured text in a sed replacement. This example converts a date from YYYY-MM-DD to DD/MM/YYYY.

echo '2026-09-24' | sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\3\/\2\/\1/'
24/09/2026

The three groups match the year, month, and day. The replacement writes groups 3, 2, and 1 in that order, with slashes between them.

Bash Shell command sed