简体   繁体   English

在bash脚本中编辑字符串变量

[英]Editting a string variable in a bash script

I have a bash script which queries a postgresql database (fyi, it is a SOGo groupware database) and gets a string of text into a variable. 我有一个bash脚本,它查询一个postgresql数据库(fyi,它是一个SOGo组件数据库),并将一串文本输入到一个变量中。 The value of the variable (let's call it teststr) looks like this: 变量的值(我们称之为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)

Now, what I want to do is remove all the data from the beginning all the way until the opening curly brace(ie remove c_defaults and all the dashes and one space just before the beginning of the curly brace) and remove the data at the end after the closing curly brace (ie the whitespace and the '(1 row)' string). 现在,我要做的是从头开始删除所有数据,直到开始大括号为止(即删除c_defaults以及所有花括号和刚好在大括号开头之前的一个空格),然后在末尾删除数据在右花括号后(即,空格和'(1 row)'字符串)之后。

At the end, the teststr string should only have the values inside the curly braces. 最后,teststr字符串应仅在花括号内包含值。 Is there any sort of regex/awking that can be done to edit this string? 是否可以进行某种形式的正则表达式/校验来编辑此字符串?

Edit 编辑

I tried the following steps: Wrote the variable called teststr into a file. 我尝试了以下步骤:将名为teststr的变量写入文件中。 catted and awked the file from $3 to $187 (for this example) into another file and read that into a variable. 将文件从$ 3 唤醒,并从$ 187 唤醒 (对于此示例)为另一个文件,并将其读入变量。 But the size isn't always going to remain the same as values inside the curly braces will surely increase as the user saves more options. 但是大小并不总是会保持不变,因为随着用户保存更多选项,花括号内的值肯定会增加。

您可以使用sed删除前{和后}所有内容:

sed -e 's/^[^{]*//g' -e 's/}[^}]*$/}/' file

Use sed to do an in-place edit. 使用sed进行就地编辑。 ( -i option). -i选项)。 This can be called from bash. 可以从bash调用它。

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

Use Perl to do an in-place edit. 使用Perl进行就地编辑。 ( -i option). -i选项)。 Invoked by bash. 由bash调用。

> perl -pi -e 's/c_defaults\s[-\s]+//g' filename

You could just match the container with sed: 您可以将容器与sed匹配:

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

If you get data from a command you could just pipe it: 如果您从命令中获取数据,则可以通过管道进行传输:

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

And save it to a variable: 并将其保存到变量中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM