代码之家  ›  专栏  ›  技术社区  ›  Brad Solomon

使用sed/awk在目录之间添加条

  •  0
  • Brad Solomon  · 技术社区  · 7 年前

    我用的是 tree 用于递归显示如下目录的实用程序:

    $ tree -F -a --dirsfirst project
    project
    ├── my_app/
    │   └── __init__.py
    └── tests/
        ├── integration/
        │   ├── __init__.py
        │   └── test_integration.py
        └── unit/
            ├── __init__.py
            └── test_sum.py
    
    $ tree -F -a --dirsfirst helloworld
    helloworld
    ├── helloworld/
    │   ├── __init__.py
    │   ├── helloworld.py
    │   └── helpers.py
    ├── tests/
    │   ├── helloworld_tests.py
    │   └── helpers_tests.py
    ├── .gitignore
    ├── LICENSE
    ├── README.md
    ├── requirements.txt
    └── setup.py
    

    我想用管道把这个输送到 sed awk 做一点小小的改变: 在任何目录列表的末尾 ,新行+文字 | 插入:

    project/
    ├── my_app/
    │   └── __init__.py
    │
    └── tests/
        ├── integration/
        |   ├── __init__.py
        |   └── test_integration.py
        |
        └── unit/
            ├── __init__.py
            └── test_sum.py
    
    helloworld/
    ├── helloworld/
    │   ├── __init__.py
    │   ├── helloworld.py
    │   └── helpers.py
    │
    ├── tests/
    │   ├── helloworld_tests.py
    │   └── helpers_tests.py
    │
    ├── .gitignore
    ├── LICENSE
    ├── README.md
    ├── requirements.txt
    └── setup.py
    

    我怎么做这个替代品?


    当前尝试&逻辑:新线+ | 在前导后插入 │ 在领头羊面前 ├── (这两个选项前的可选空白):

    tree -F -a --dirsfirst helloworld | sed -E 's/^\s*\│.*\n\s*\├──/???/g'
    

    可怕地被困在那里——Python的熟练程度提高了100倍 re 还有一些 grep 而不是sed/awk。

    2 回复  |  直到 7 年前
        1
  •  3
  •   melpomene    7 年前

    下面是一个使用Perl的可能解决方案:

    perl -pe 'if (/│.*└/) { print; s/ *└.*// }'
    

    想法:
    每行包含 │ └ 之后的某个地方,特里姆 └ 以及后面的所有字符和前面的所有空格,然后输出修改后的行。

    效果:

    │    │   └── foo.xyz
    

    之后将出现一个新行,其中包含

    │    │
    

    在输出中。

    Sed版本:

    sed '/│.*└/{p;s/ *└.*//}'
    

    对于示例输入,它会生成以下输出:

    $ tree -F -a --dirsfirst project
    project
    ├── my_app/
    │   └── __init__.py
    │
    └── tests/
        ├── integration/
        │   ├── __init__.py
        │   └── test_integration.py
        │
        └── unit/
            ├── __init__.py
            └── test_sum.py
    
    $ tree -F -a --dirsfirst helloworld
    helloworld
    ├── helloworld/
    │   ├── __init__.py
    │   ├── helloworld.py
    │   └── helpers.py
    │
    ├── tests/
    │   ├── helloworld_tests.py
    │   └── helpers_tests.py
    │
    ├── .gitignore
    ├── LICENSE
    ├── README.md
    ├── requirements.txt
    └── setup.py
    
        2
  •  2
  •   Ed Morton    7 年前

    使用 cat file 用GNU awk代替问题中的两个树命令:

    $ cat file | awk '1; match($0,/^((\s*│)+)\s+└/,a){ print a[1] }'
    project
    ├── my_app/
    │   └── __init__.py
    │
    └── tests/
        ├── integration/
        │   ├── __init__.py
        │   └── test_integration.py
        │
        └── unit/
            ├── __init__.py
            └── test_sum.py
    
    helloworld
    ├── helloworld/
    │   ├── __init__.py
    │   ├── helloworld.py
    │   └── helpers.py
    │
    ├── tests/
    │   ├── helloworld_tests.py
    │   └── helpers_tests.py
    │
    ├── .gitignore
    ├── LICENSE
    ├── README.md
    ├── requirements.txt
    └── setup.py
    

    对于任何awk:

    $ cat file | awk '1; match($0,/^[[:space:]]*(│[[:space:]]+)+└/){ print substr($0,1,RLENGTH-1) }'
    

    使用GNU sed:

    $ cat file | sed -En 'p; s/^((\s*│)+)\s+└.*/\1/p'
    
        3
  •  0
  •   potong    7 年前

    这可能适用于您(GNU-sed):

    sed 's/\(.*│\)\s*└──.*/&\n\1/' file
    

    其中文件可以是树命令管道中的标准格式,例如。

    tree -Fa --dirsfirst helloworld | sed 's/\(.*│\)\s*└──.*/&\n\1/'
    
    推荐文章