Home  >  Q&A  >  body text

linux - 如何简化处理路径替换

我有一个 /tmp/aa/bb.txt 的文件。我现在要把这个路径替换成 tmp-aa-bb.txt

现在的处理方法是 echo "/tmp/aa/bb.txt" | sed "s/\//-/g" | cut -b 2-

想问下是否有命令可以直接匹配替换后面部分。

PHPzPHPz2723 days ago818

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 12:01:41

    echo "/tmp/aa/bb.txt" | tr "/" "-" | cut -b 2-
    

    Is this so?


    If you don’t want to match the first character, then regular negative pre-checking can also be done. It's a pity that sed doesn't support it, that's how it is with Python

    # encoding: UTF-8
    import re
    print re.sub(r'(?!^)\/','-', '/tmp/aa/bb.txt')
    

    Output/tmp-aa-bb.txt

    The more complex the statement, the lower the maintainability, so why bother.

    reply
    0
  • Cancelreply