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

Insert a dash after the first character:

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

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

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

Remove a range of characters:

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

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

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

Leave a comment

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