How does this sed line works :

echo lred_6607: broken symbolic link to ./safaribooks/red_6607 |
sed -e 's/^\|: *broken symbolic.*$/"/g'

1.There is no | character in the output of previous command.

2.what is the use of ^ and $

3.How does the output is prefixed and suffixed by a pair of double quotes

output : "lred_6607"

1 Answer 1


This sed line selects sequences of chars and replace them with ". The parts of the line which are not matched are left unchanged.

\| means or, so it separates 2 regexes.

Any sequence of chars matched by these 2 regexes is replaced with ".

The first is ^. It matches the start of the line. If start of the line is encountered, it is replaced with "

The second regex is : *broken symbolic.*$: it means any string starting with : and zero or any number of space chars (*), and broken symbolic char sequence, and followed with .* which means zero or any number of any chars, up to the end of line $. If encountered it is replaced with ".

lred_6607 is not matched at all, so it is left unchanged.

The result is "lred_6607"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .