使用bash中的正则表达式从字符串中提取信息

时间:2013-08-14 07:04:25

标签: regex bash search

我在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值?
  • 我应该将它们提取到新的字符串中,编辑它然后再写回来还是有更有效的方法?

2 个答案:

答案 0 :(得分:2)

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

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

#!/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对象。

答案 1 :(得分:0)

public List<Header> getAllHeaders(String module) { List<Module> modules = entityManager.createQuery("select m from Module m",Module.class).getResultList(); List<Header> headers = new ArrayList<Header>(); for (Module m : modules){ if (m.getModuleName().equals(module)){ for(Header h : m.getHeaders()) { headers.add(h); for(Row q : h.getRows()) { } } } } return headers; } 是一个很好的解析和编辑JSON的工具,很容易从shell驱动。

jq

......或者,一次完成整件事:

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