How does this Sed command work? [closed]

1

I wanted to know how this command works letter by letter, I do not understand it.

find <directorio> -type d | sed -e 's;[^/]*/;|____;g;s;____|; |;g'

The part of find <directorio> -type d I understand it well, which would be to search all subdirectories from a directory. What I do not understand how it works is sed -e 's;[^/]*/;|____;g;s;____|; |;g' .

    
asked by Guido Modarelli 06.09.2018 в 14:48
source

2 answers

2

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
source
2

Sed is a% non-interactive% share editor. Take a text entry, either of stream or of a file, perform certain operations on specified lines of the entry, one line at a time, then issue the result to stdin or to a file.

The option:

**-e** script, **--expression**=script  

Take the 'script' script to be executed.

Example (taken from here ) :

All these operations are equivalent. Two operations are performed: one removes any line that matches the regular expression stdout , and the other replaces all the occurrences of the string 'hello' with 'world':

sed '/^foo/d ; s/hello/world/' input.txt > output.txt

sed -e '/^foo/d' -e 's/hello/world/' input.txt > output.txt

echo '/^foo/d' > script.sed
echo 's/hello/world/' >> script.sed
sed -f script.sed input.txt > output.txt

echo 's/hello/world/' > script2.sed
sed -e '/^foo/d' -f script2.sed input.txt > output.txt

Here you have the complete documentation .

    
answered by 06.09.2018 в 15:36