0

我在 bash 中有一个字符串变量,如下所示:

{"SOGoTimeFormat": "%H:%M", "SOGoMailShowSubscribedFoldersOnly": "0", "SOGoMailSignaturePlacement": "below", "SOGoLanguage": "English", "SOGoDayEndTime": "18:00", "SOGoDefaultCalendar": "selected", "SOGoFirstWeekOfYear": "January1", "SOGoFirstDayOfWeek": "0", "SOGoTimeZone": "Asia\/Kolkata", "SOGoContactsCategories": ["Business Partner", "Colleague", "Competitor", "Customer", "Family", "Friend", "Press", "Provider", "VIP"], "Vacation": {"enabled": 0, "endDate": 1374690600, "autoReplyEmailAddresses": ["testuser@testdomain.com"], "ignoreLists": 1, "autoReplyText": "", "daysBetweenResponse": "7", "endDateEnabled": 0}, "SOGoCalendarTasksDefaultClassification": "PUBLIC", "SOGoMailSortByThreads": "0", "SOGoMailMessageCheck": "manually", "SOGoMailMessageForwarding": "inline", "SOGoLoginModule": "Mail", "SOGoCalendarCategoriesColors": {"Customer": "#aaa", "Calls": "#aaa", "Favorites": "#aaa", "Meeting": "#aaa", "Ideas": "#aaa", "Miscellaneous": "#aaa", "Birthday": "#aaa", "Anniversary": "#aaa", "Vacation": "#aaa", "Travel": "#aaa", "Projects": "#aaa", "Suppliers": "#aaa", "Gifts": "#aaa", "Clients": "#aaa", "Issues": "#aaa", "Business": "#aaa", "Holidays": "#aaa", "Personal": "#aaa", "Status": "#aaa", "Public Holiday": "#aaa", "Follow up": "#aaa", "Competition": "#aaa"}, "SOGoBusyOffHours": "0", "SOGoCalendarCategories": ["Customer", "Calls", "Favorites", "Meeting", "Ideas", "Miscellaneous", "Birthday", "Anniversary", "Vacation", "Travel", "Projects", "Suppliers", "Gifts", "Clients", "Issues", "Business", "Holidays", "Personal", "Status", "Competition", "Follow up", "Public Holiday"], "SOGoCalendarEventsDefaultClassification": "PUBLIC", "Forward": {"enabled": 1, "forwardAddress": ["testuser1@testdomain.com", "testuser2@testdomain.com"], "keepCopy": 1}, "SOGoRememberLastModule": "0", "SOGoMailReplyPlacement": "below", "SOGoMailDisplayRemoteInlineImages": "never", "SOGoSieveFilters": [{"actions": [{"method": "fileinto", "argument": "INBOX\/spam"}], "active": 1, "rules": [{"operator": "contains", "field": "subject", "value": "[SPAM]"}], "match": "any", "name": "spam"}, {"actions": [{"method": "fileinto", "argument": "INBOX\/spam"}], "active": 1, "rules": [{"operator": "contains", "field": "subject", "value": "TESTTEST"}], "match": "any", "name": "new"}], "SOGoDayStartTime": "08:00", "SOGoMailComposeMessageType": "text"}

它是单行文本,没有换行或任何东西。我想要实现的是,这里有一个名为"Forward". 如果对应的enabled值为0,则什么也不做。如果它对应的enabled值为1,它应该在里面forwardAddress一个一个地解析里面的电子邮件地址并根据一些比较删除一个(在这个字符串中,假设我们要删除testuser2)。

我有两个问题:

  • 如何使用正则表达式查找"Forward"然后检查enabled值来实现这一点?
  • 我应该将它们提取到一个新字符串中,对其进行编辑然后将其写回还是有更有效的方法?
4

2 回答 2

2

你拥有的是 JSON,你应该使用的是 JSON 解析器。使用正则表达式不是一个好的替代品。

这是一些加载字符串的 python,如果enabledinForward为 1,则从列表中删除任何带有子字符串“testuser2”的地址forwardAddress

#!/bin/python
import sys
import json

thing = json.load(sys.stdin)
forward = thing["Forward"]

if forward["enabled"] == 1:
    forward["forwardAddress"] = \
        filter(lambda x: not "testuser2" in x, \
            forward["forwardAddress"])

json.dump(thing, sys.stdout)

你可以运行它

echo "$yourvariable" | python thisfile.py

json 重新编码过程可能会打乱字段。这无关紧要,因为字符串仍然代表相同的 json 对象。

于 2013-08-14T07:30:31.060 回答
0

jq是解析和编辑 JSON 的优秀工具,易于从 shell 驱动。

# extract "enabled" field from "Forward"
enabled=$(jq '.Forward.enabled` <input.json)

...或者,一次完成整个事情:

jq '
  if .Forward.enabled==1 then
    .Forward.forwardAddress=[(
      .Forward.forwardAddress[] |
      select(. | test("testuser2") | not)
    )]
  else . end
' <input.json >output.json
于 2015-11-03T19:28:01.563 回答