sed allows specifying which occurence of a pattern (in a line) should be replaced:
echo AAAAA | sed 's/A/X/'
echo AAAAA | sed 's/A/X/g'
echo AAAAA | sed 's/A/X/3'
To specifically target your problem with this method, you will need two modifications in
sed: 1) change the beginning of the line (
^) to add the opening parentheses, 2) replaces the first dash by the closing parentheses and a space.
echo 123-123-1234 | sed 's/^/(/;s/-/) /'
sed allows for matching patterns and saving them (by number of occurrence) by enclosing the pattern in parentheses. In the output string you can call them by their number:
echo 123-123-1234 | sed 's/\([0-9]\+\)-\([0-9]\+\)-\([0-9]\+\)/(\1) \2-\3/'
[0-9] matches a digit between 0 and 9,
\+ means one or more occurrences of the previous group (GNU extension to
\(<pattern>\) will save the pattern and give it an index number (starting with 1 for the first and so on). Thus
\([0-9]\+\)- will match one or more digits followed by a dash and will remember the digits part.
In the output
\1 prints the first saved pattern from the match (i.e.
[0-9]\+ ... one or more digits before the a dash. We then just use the three saved digit groups and format them as we desire.