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"}

これは、折り返しなどではなく、1 行のテキストです。私が達成しようとしているのは、これには と呼ばれるフィールドがあるということ"Forward"です。対応するenabled値が 0 の場合、何もしません。対応するenabled値が 1 の場合、内部の電子メール アドレスを 1 つずつ解析し、forwardAddress何らかの比較に基づいて 1 つを削除する必要があります (この文字列で、testuser2 を削除するとします)。

2 つの質問があります。

  • 正規表現を使用して値を見つけ"Forward"て確認するにはどうすればよいenabledですか?
  • それらを新しい文字列に抽出し、編集してから書き戻す必要がありますか、それともより効率的な方法がありますか?
4

2 に答える 2

2

あなたが持っているのは JSON であり、使用すべきものは JSON パーサーです。正規表現の使用は適切な代替手段ではありません。

enabled文字列をロードし、 inが 1 の場合、部分文字列「testuser2」を含むアドレスをリストForwardから削除するpython を次に示します。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

jqJSON を解析および編集するための優れたツールであり、シェルから簡単に操作できます。

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

...または、すべてを 1 回のパスで実行するには:

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 に答える