我正在尝试使用BASH将带有空格的变量传递给sed,并且在提示符下,它可以正常工作:
$ tmp=/folder1/This Folder Here/randomfile.abc
$ echo "$tmp" | sed -e 's/ /\\ /g'
/folder1/This\ Folder\ Here/randomfile.abc
Run Code Online (Sandbox Code Playgroud)
但是一旦我将它传递给变量,sed就不再用反斜杠替换空格:
$ tmp=/folder1/This Folder Here/randomfile.abc
$ location=`echo "$tmp" | sed -e 's/ /\\ /g'`
$ echo $location
/folder1/This Folder Here/randomfile.abc
Run Code Online (Sandbox Code Playgroud)
我希望第二双眼睛可以接受我不喜欢的东西.
我知道这对你们大多数人来说都是基本的,但是请耐心等待我,我正试图将蜘蛛网弄脏,并建立肌肉记忆.
我有一个包含主机名密钥和列表值的主机dict.我希望能够从每个值的列表中删除任何fruit_项.
host = {
'abc.com': ['fruit_apple', 'fruit_orange', 'veg_carrots'],
'123.com': None,
'foo.com': ['fruit_tomatoes', 'veg_potatoes']
}
for v in host.values():
if v is not None:
for x in v:
try:
# creating my filter
if x.startswith('fruit_'):
# if x finds my search, get, or remove from list value
host(or host.value()?).get/remove(x)# this is where i'm stuck
print(hr.values(#call position here?)) # prove it
except:
pass
Run Code Online (Sandbox Code Playgroud)
我被困在评论区域,我觉得我错过了另一个迭代(某个地方的新列表?),或者我可能不理解如何写回列表值.任何方向都会有所帮助.