在bash使用正则表达式从字符串中提取信息 [英] Extracting information from a string using regex in bash

查看:1374
本文介绍了在bash使用正则表达式从字符串中提取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在bash一个字符串变量看起来像这样:

I have a string variable in bash which looks like so:

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

这是文本,而不是包裹或任何一行。我试图实现的是,有这名为前进字段。如果它对应的启用值为0,什么也不做。如果它对应的启用值是1,它应该在解析里面 forwardAddress 的电子邮件地址中的一个接一个内的基础上删除了一些比较。(在此字符串,假设我们要删除testuser2)。

It is a single line of text, not wrapped or anything. What I am trying to achieve is, there is a field in this called "Forward". If it's corresponding enabled value is 0, do nothing. If it's corresponding enabled value is 1, it should the parse the email addresses inside forwardAddress one-by-one inside and delete one based on some comparison (In this string, let's say we want to delete testuser2).

我有两个问题:


  • 如何实现这一目标使用正则表达式找到前进然后检查启用价值?

  • 我应该解压缩到一个新的字符串,编辑,然后将它写回还是有一个更有效的方法?

  • How do I achieve this using regex to find "Forward" and then check the enabled value?
  • Should I extract them into a new string, edit it and then write it back or is there a more efficient method?

推荐答案

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

What you have is JSON and what you should be using is a JSON parser. Using regex is not a good substitute.

下面是一些Python加载的字符串,如果启用转发 1,删除的任何地址从 forwardAddress 列表子testuser2

Here's some python that loads the string, and if enabled in Forward is 1, deletes any address with the substring "testuser2" from the forwardAddress list:

#!/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的重新编码过程可能洗牌的字段。这不要紧,因为琴弦还是重新present同JSON对象。

The json re-encoding process might shuffle the fields. This doesn't matter, as the strings still represent the same json objects.

这篇关于在bash使用正则表达式从字符串中提取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆