[SOLVED] Want to grep data between square brackets(Including brackets)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Want to grep data between square brackets(Including brackets)
I am trying to awk data from a curl output.
My requirement is to grep data between square brackets [ ].
Eg: In a file I have a output as {"command1":{"cmd":"get_msp_audit_export","response":[{"eventdate":"2018-04-06","facility_fee":null]},"header":{"src_sys_name":"sharedservices","ver":"1.0","archtics_version":"v999","ats_version":"4.4 2.0","src_sys_type":"2"}} and so on, from this I want grep the output as [{"eventdate":"2018-04-06","facility_fee":null] only(Including square brackets).
The occurrence of square brackets is only one.
I tried the below command but removes the square brackets too. Please help ASAP.
No, Actually I want the data between square brackets only(output should have square brackets too), starting from [ and ending with ]. For my eg output should come as [{"eventdate":"2018-04-06","facility_fee":null]
I Tried awk '$0=="[" {p=1}; p; $0=="]" {p=0}' and awk 'NR>1{print $1}' RS=[ FS=]
First command prints a blank output and second command prints the output as {"eventdate":"2018-04-06","facility_fee":null. But I want the output as [{"eventdate":"2018-04-06","facility_fee":null]
if you want to remove every insistence of [something] and remove everything else, but if you got multiple
[something][something][something]
Code:
$ echo -e $string | sed 's/.*\[\([^]]*\)\].*/\1/g'
{eventdate:2018-04-06,facility_fee:null
#gets the first instance of [ something ] ..
#you'll just need to put the [ ] back
$ string={"command1":{"cmd":"get_msp_audit_export","response":[{"eventdate":"2018-04-06","facility_fee":null]},"header":{"src_sys_name":"sharedservices","ver":"1.0","archtics_version":"v999","ats_version":"4.4 2.0","src_sys_type":"2"}}
figure out how to work down your string to get the next insistence of [ something ]
@danielbmartin - Your awk scripts do work but I'm having trouble understanding how -F"[][]" is evaluated. The manpage says:
Quote:
If FS is a single character, fields are separated by that character. If FS is the null string, then each individual character becomes a separate field. Otherwise, FS is expected to be a full regular expression.
@danielbmartin - Your awk scripts do work but I'm having trouble understanding how -F"[][]" is evaluated.
I hadn't read the manpage. Now that I have, I don't understand it.
This awk ...
Code:
awk -F"[dh]" '{print $2}' <<<"abcdefghi"
... says there are two Field Separator characters, d and h.
The result is efg
This awk ...
Code:
awk -F"[][]" '{print $2}' <<<"abc[efg]i"
... says there are two Field Separator characters, ] and [.
The result is efg
There is one "wrinkle" to note. Coding this: -F"[[]]" doesn't work because it looks like the null string, so you reverse the Field Separator brackets and code -F"[][]" instead.
Daniel B. Martin
.
Last edited by danielbmartin; 02-12-2018 at 09:59 PM.
Reason: Cosmetic improvement
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.