linux - 如何简化处理路径替换
PHPz
PHPz 2017-04-17 11:59:41
0
1
816

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

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

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

PHPz
PHPz

学习是最好的投资!

reply all(1)
洪涛
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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!