使用sed和find命令的树功能 [英] Tree functionality using sed and find command

查看:280
本文介绍了使用sed和find命令的树功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  find。 -type d -print | sed -e's; [^ /] * /; | ____; g; s; ____ | |; g'

通过unix.com上的命令行,在UNIX中充当树命令。
我想要有人解释一下sed命令到底在做什么。
我无法理解语法



现在我知道基本的sed命令语法。

PS:命令完美无缺,论坛在论坛中关闭。


< find 命令只查找目录并打印它们。



sed 命令如下:

  sed -e's; [^ /] * /; | ____; g; s; ____ |; |; g'
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^
| |
|替换____ |用|
将所有内容替换为最后一个/ with ____

让我们看一下action中的命令step by步骤:

找到所有的目录并打印它的名字:

pre $ $ find。 -type d -print

./python
./python/mytest
./python/mytest2
./python/mytest2/bk
./python/mytest2/infiles
./bash
./bash/backup
./awk
./sed

____ 替换最后一个 /


  • sed -e's; [^ /] * /; | ____; g'

    • [^ /] * - >获取所有字符( * )不同于 / 。请注意, ^ 代表否定。

    • / - >匹配一个 /

    • 给出该块,用字符串 | ____




查看示例:

  $ echohello / how / are / you| sed's; [^ /] * / ;; g'
you

$ echohello / how / are / you| sed's; [^ /] * /; | ____; g'
| ____ | ____ | ____ you

在这种情况下:

  $ find。 -type d -print | sed -e's; [^ /] * /; | ____; g'

| ____ python
| ____ | ____ mytest
| ____ | ____ mytes2
| ____ | ____ | ____ bk
| ____ | ____ | ____ infiles
| ____ bash
| ____ | ____备份
| ____ awk
| ____ sed

替换 ____ | with |

  $ find。 -type d -print | sed -e's; [^ /] * /; | ____; g; s; ____ | |; g'

| ____ python
| | ____ mytest
| | ____ mytest2
| | | ____ bk
| | | ____ infiles
| ____ bash
| | ____备份
| ____ awk
| ____ sed

我们通常会看到 sed 表达式:

  sed's / hello / bye / g'

但是如果您怀疑 / 可以使用不同的分隔符有问题。所以在这种情况下,分隔符是;

  sed's; hello; bye; g'

最后请注意:

  sed -e's; [^ /] * /; | ____; g; s; ____ | |; g'
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^
第一命令第二命令

意味着两个命令是要被执行的一个在另一个之后,所以它等效于:

  sed's; [^ /] * /; | ____; g'| sed's; ____ |; |; g'


find . -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'

Came across this commandline on unix.com which acts as tree command in unix. I want to someone to explain what that sed command is exactly doing. i'm unable to understand the syntax

Right now i know the basic sed command syntax. I'm still getting a hang of the unix environment .

PS: Command works perfectly and The discussion was closed in that forum.

解决方案

The find command is only looking for directories and printing them.

sed command is as follows:

sed -e 's;[^/]*/;|____;g;s;____|; |;g'
        ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^
         |               |
         |               replace ____| with  |
         replace everything up to the last / with ____

Let's see the command in action step by step:

Find all the directories and print its name:

$ find . -type d -print 
.
./python
./python/mytest
./python/mytest2
./python/mytest2/bk
./python/mytest2/infiles
./bash
./bash/backup
./awk
./sed

Replace everything up to the last / with ____:

  • sed -e 's;[^/]*/;|____;g' is read like this:
    • [^/]* --> get all characters as possible (*) that are different than /. Note that ^ stands for negation here.
    • / --> match a /.
    • given that block, replace it with the string |____.

See an example:

$ echo "hello/how/are/you" | sed 's;[^/]*/;;g'
you

$ echo "hello/how/are/you" | sed 's;[^/]*/;|____;g'
|____|____|____you

In this case:

$ find . -type d -print | sed -e 's;[^/]*/;|____;g'
.
|____python
|____|____mytest
|____|____mytes2
|____|____|____bk
|____|____|____infiles
|____bash
|____|____backup
|____awk
|____sed

Replace ____| with |:

$ find . -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
.
|____python
| |____mytest
| |____mytest2
| | |____bk
| | |____infiles
|____bash
| |____backup
|____awk
|____sed

We normally see sed expressions like:

sed 's/hello/bye/g'

but you can use different delimiters if you suspect / can be problematic. So in this case the delimiter is ;:

sed 's;hello;bye;g'

Finally, note that:

sed -e 's;[^/]*/;|____;g;s;____|; |;g'
        ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^
         1st command     2nd command

means that two commands are to be executed one after the other, so it is equivalent to:

sed 's;[^/]*/;|____;g' | sed 's;____|; |;g'

这篇关于使用sed和find命令的树功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆