简体   繁体   English

使用sed或python将所有反斜杠替换为正斜杠

[英]To replace all backslashes with forward slashes using sed or python

aim: to replace all backslashes with forward slashes. 目标:用正斜杠替换所有反斜杠。

actual string:\\\\test.abc.com\\path1\\path1_1\\123\\ubntu 实际字符串:\\\\ test.abc.com \\ path1 \\ path1_1 \\ 123 \\ ubntu

expected string://test.abc.com/path1/path1_1/123/ubntu 预期的字符串://test.abc.com/path1/path1_1/123/ubntu

trying to get sed to work for the above is turning out to be quite a task 试图让sed满足上述要求已成为一项艰巨的任务

any thoughts? 有什么想法吗?

Edit: read as Env variables in Jenkins The Complete use case being: They are passed as Windows UNC Paths via jenkins, need to be mounted on Unix 编辑:在Jenkins中读取为Env变量 完整的用例是:它们通过jenkins作为Windows UNC路径传递,需要在Unix上安装

In Python on *nix 在* nix上的Python中

It behaves differently when tried via variables!, when i read the variables via python, the '\\' string is chopped of by the interpreter. 通过变量尝试时,其行为会有所不同!,当我通过python读取变量时,解释器将'\\'字符串切掉。 i have tried repr(variable) in conjunction with .decode('string_escape') but to no avail. 我曾尝试与.decode('string_escape')结合使用repr(variable),但无济于事。 raw_input() does not help in python on unix raw_input()在Unix上的python中无济于事

here is main the python script named test1.py 这是主要的名为test1.py的python脚本

s=re.sub(r'\\',r'/',repr(sys.argv[1].decode('string_escape')), Which is run as python test1.py \\test.abc.com\\path1\\path1_1\\123\\ubntu ,Output of which is turning out to be //test.abc.compath1path1_1123ubntu; Why is python thinking '\\' cannot be displayed :-( – s = re.sub(r'\\',r'/',repr(sys.argv [1] .decode('string_escape')),以python test1.py \\ test.abc.com \\ path1 \\运行path1_1 \\ 123 \\ ubntu,其输出结果为//test.abc.compath1path1_1123ubntu;为什么python不能显示'\\':-( –

Trying via sed now through env variables. 现在尝试通过env变量通过sed进行操作。

You can use other tools but translating characters is the job tr was created for: 您可以使用其他工具,但转换的字符是工作tr是为创建:

$ tr '\\' '/' < file
//test.abc.com/path1/path1_1/123/ubntu

Updated for environment variables. 更新了环境变量。

You say that the path is in an environment variable (let's say UNC_PATH), so in Python: 您说路径在环境变量中(假设为UNC_PATH),因此在Python中:

# test.py
import re, sys

s = re.sub(r'\\', r'/', sys.argv[1])
print s

and invoke it like this: 并像这样调用它:

$ UNC_PATH=\\\\test.abc.com\\path1\\path1_1\\123\\ubntu
$ python test.py $UNC_PATH
//test.abc.com/path1/path1_1/123/ubntu

For sed, do this: 对于sed,请执行以下操作:

$ echo $UNC_PATH | sed -e 's/\\/\//g'
//test.abc.com/path1/path1_1/123/ubntu

Original answer 原始答案

With sed: 与sed:

$ echo '\\test.abc.com\path1\path1_1\123\ubntu' | sed -e 's/\\/\//g' 
//test.abc.com/path1/path1_1/123/ubntu

In Python: 在Python中:

>>> import re
>>> s = re.sub(r'\\', r'/', r'\\test.abc.com\path1\path1_1\123\ubntu')
>>> print s
//test.abc.com/path1/path1_1/123/ubntu

Through sed, 通过sed

$ sed 's~\\~/~g' file
//test.abc.com/path1/path1_1/123/ubntu

Through Python, 通过Python,

>>> import re
>>> s = r'\\test.abc.com\path1\path1_1\123\ubntu'
>>> print s
\\test.abc.com\path1\path1_1\123\ubntu
>>> m = re.sub(r'\\', r'/', s)
>>> print m
//test.abc.com/path1/path1_1/123/ubntu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM