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-/'
3-94CV00897PCD

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

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

Remove a range of characters:

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

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

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).