簡體   English   中英

使用bash中的正則表達式從字符串中提取信息

[英]Extracting information from a string using regex in bash

我在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值?
  • 我應該將它們提取到一個新字符串中,編輯它然后再寫回來還是有更有效的方法?

你擁有的是JSON,你應該使用的是一個JSON解析器。 使用正則表達式不是一個好的替代品。

這是一些加載字符串的python,如果在Forward enabled為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對象。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM