regex


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. The value of the variable (let's call it teststr) looks like this:
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(i.e 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 (i.e. the whitespace and the '(1 row)' string).
At the end, the teststr string should only have the values inside the curly braces. 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. catted and awked the file from $3 to $187 (for this example) into another file and read that into a variable. 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.
You can use sed to remove anything up to the first { and everything after }:
sed -e 's/^[^{]*//g' -e 's/}[^}]*$/}/' file
Use sed to do an in-place edit. (-i option). This can be called from bash.
sed -i -e 's/c_defaults\s[-\s]+//g' filename
Use Perl to do an in-place edit. (-i option). Invoked by bash.
> perl -pi -e 's/c_defaults\s[-\s]+//g' filename
You could just match the container with 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; }')

Related Links

How to find a regex in re2 to search between strings?
How to combine two lists in one list? using notepad++ regex? [duplicate]
Scala - Explanation for regex statement
Python Regular Expression Multiple Groups
Using regexp in Matlab to find specific file from beginning and end components of filename
regular expression to check ends with a digit
Regex to capture string until another string is encountered
Google apps script, getBody() from Gmail, regex \n
How can I do a search across multilines in Delphi's regex
strip a regex character match from a string
Replacement within the matched string with sed
AHK - How to use dynamic hotstring with a barcode scanner
Fuzzy match on google sheets
VB Net RegEx Match Multiple
Invalid Escape Character in Regex for html 5 Input Validation
Regex to match start and end line [duplicate]

Categories

HOME
html5-canvas
sqlite
unit-testing
clojure
bootstrap-4
identityserver4
xaml
bootstrap-selectpicker
weight
localforage
prometheus
visual-studio-2012
webdriver-io
pyusb
jfreechart
entity
python-xarray
apache-spark-sql
tcplistener
zebble
cobol
luci
condor
apex
git-svn
rgb
orchardcms-1.10
lookup-tables
pydev
similarity
django-users
google-shared-contacts
capstone
jupyter-irkernel
spring-saml
ipod-touch
protobuf-3
mdanalysis
qmenubar
powerpc
simplesamlphp
asyncsocket
uservoice
website-hosting
mrtg
formstack
complement
billing
angular-directive
jcuda
manifoldcf
nsd
amazon-dynamodb-streams
ocsp
xnamespace
file-management
jstorm
winobjc
webmail
uiswitch
aplpy
php-opencloud
wgs84
pypdf
coldfusion-10
hjson
tabbar
nbug
telescope
gem
gevent-socketio
diagonal
zim-database
physx
private-methods
iphone-privateapi
zeroclipboard
fig
xml.modify
antisamy
pydatalog
mongo-shell
array-address
jquery-mobile-popup
android-lru-cache
str-to-date
buildout
gora
hla
couchpotato
google-chrome-frame
dependency-walker
hudson-plugins
rc-shell
cellphone
httpconnection
android-assets
process.start
boost-gil
springboard
bubble-popup
high-traffic
n900
custom-protocol
iequalitycomparer
dynamic-websites
reference-library

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App