[英]Editting a string variable in a bash script
我有一個bash腳本,它查詢一個postgresql數據庫(fyi,它是一個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 row)'字符串)之后。
最后,teststr字符串應僅在花括號內包含值。 是否可以進行某種形式的正則表達式/校驗來編輯此字符串?
編輯
我嘗試了以下步驟:將名為teststr的變量寫入文件中。 將文件從$ 3 喚醒,並從$ 187 喚醒 (對於此示例)為另一個文件,並將其讀入變量。 但是大小並不總是會保持不變,因為隨着用戶保存更多選項,花括號內的值肯定會增加。
您可以使用sed
刪除前{
和后}
所有內容:
sed -e 's/^[^{]*//g' -e 's/}[^}]*$/}/' file
使用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
您可以將容器與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; }')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.