小编hob*_*bes的帖子

传递SED替换BASH变量中的参数

我正在尝试使用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)

我希望第二双眼睛可以接受我不喜欢的东西.

bash sed

4
推荐指数
1
解决办法
173
查看次数

如何从dict值中删除列表项?

我知道这对你们大多数人来说都是基本的,但是请耐心等待我,我正试图将蜘蛛网弄脏,并建立肌肉记忆.

我有一个包含主机名密钥和列表值的主机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)

我被困在评论区域,我觉得我错过了另一个迭代(某个地方的新列表?),或者我可能不理解如何写回列表值.任何方向都会有所帮助.

python dictionary list python-3.x

4
推荐指数
1
解决办法
79
查看次数

标签 统计

bash ×1

dictionary ×1

list ×1

python ×1

python-3.x ×1

sed ×1