我有一个 /tmp/aa/bb.txt 的文件。我现在要把这个路径替换成 tmp-aa-bb.txt
现在的处理方法是 echo "/tmp/aa/bb.txt" | sed "s/\//-/g" | cut -b 2-
想问下是否有命令可以直接匹配替换后面部分。
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.