This command is a manual implementation of what the tree
command does, as I explained in Tree functionality using sed and find command .
When you have sed -e 's/bla/ble/g;s/x/y/g'
it means that you are putting two Sed commands at the same time: sed 's/bla/ble/g'
and sed 's/x/y/g'
, so that the resultant of the first one is processed in the second one. Therefore, let's start separating them.
sed -e 's;[^/]*/;|____;g;s;____|; |;g'
is
sed 's;[^/]*/;|____;g'
and its result apply:
sed 's;____|; |;g'
Let's go one by one: sed 's;[^/]*/;|____;g'
The basic expression of Sed is sed 's/busca/reemplazo/X'
and what it does is search "search" and replace it with "replacement" using the flags described in X. If this flag is g
, it means that it does so as many times as can Therefore, sed 's/busca/reemplazo/g'
will replace all "search" with "replacement":
$ echo "hola hola" | sed 's/hola/adios/'
adios hola
$ echo "hola hola" | sed 's/hola/adios/g'
adios adios
Also, and as I explained in How to use different separators in thirst? , Sed allows you to use other separators in place of /
, using basically any character. This is helpful for cases, like this one, in which you search for something like "/" and use that same character as a separator forcing it to escape. Therefore, these are equivalent:
sed 's/busca/reemplazo/g'
sed 's;busca;reemplazo;g'
Thus, sed 's;[^/]*/;|____;g'
what it does is:
- search
[^/]*/
. This means any character minus "/" and followed by "/".
- replaces it with
|____
.
Therefore, you are replacing all "hello /" with "|
answered by 06.09.2018 / 15:37