Sed code snippets for inserting characters at various positions in a line

Insert a dash after the first character:

echo 394CV00897PCD | sed -r 's/(.{1})/\1-/'

Insert a dash before and after a range of alpha characters:

echo 394CV00897PCD | sed -r 's/([a-z]+)/-\1-/ig'

Remove a range of characters:

echo "3:94 CV00  897(PCD)" | sed 's/[:()\ ]//g'

Replace more than a single occurrence of dash with a single dash:

echo 3-94---CV-00897--PCD | sed -r 's/([-]+)/-/g'

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).