0

我有一个 bash 脚本,它查询一个 postgresql 数据库(仅供参考,它是一个 SOGo 群件数据库)并将一串文本放入一个变量中。变量的值(我们称之为 teststr)如下所示:

c_defaults ----------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
 {"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 row)

现在,我想要做的是从一开始一直删除所有数据,直到打开花括号(即删除 c_defaults 和所有破折号以及花括号开头之前的一个空格)并删除最后的数据在右大括号之后(即空格和“(1行)”字符串)。

最后,teststr 字符串应该只有花括号内的值。是否可以使用任何正则表达式/awking 来编辑此字符串?

编辑

我尝试了以下步骤: 将名为 teststr 的变量写入文件。将文件从 $3 到 $187(对于此示例) cattedawked到另一个文件中,并将其读入变量。但是大小并不总是保持不变,因为花括号内的值肯定会随着用户保存更多选项而增加。

4

3 回答 3

2

您可以使用sed删除任何内容到第一个{和之后的所有内容}

sed -e 's/^[^{]*//g' -e 's/}[^}]*$/}/' file
于 2013-08-14T06:09:33.317 回答
1

用于sed进行就地编辑。(-i选项)。这可以从 bash 中调用。

sed -i -e 's/c_defaults\s[-\s]+//g' filename

用于Perl进行就地编辑。(-i选项)。由 bash 调用。

> perl -pi -e 's/c_defaults\s[-\s]+//g' filename
于 2013-08-14T06:08:10.173 回答
0

您可以将容器与 sed 匹配:

sed -ne '/{.*}/{ s|^[^{]*\({.*}\)[^}]*$|\1|; p; }' file

如果您从命令中获取数据,则可以通过管道传输它:

your_command a b | sed -ne '/{.*}/{ s|^[^{]*\({.*}\)[^}]*$|\1|; p; }'

并将其保存到变量中:

VAR=$(your_command a b | sed -ne '/{.*}/{ s|^[^{]*\({.*}\)[^}]*$|\1|; p; }')
于 2013-08-14T06:18:15.437 回答