LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2018, 10:28 AM   #1
harshagr
LQ Newbie
 
Registered: Feb 2018
Posts: 9

Rep: Reputation: Disabled
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.

#awk 'NR>1{print $1}' RS=[ FS=]

Last edited by harshagr; 02-12-2018 at 10:38 AM.
 
Old 02-12-2018, 10:42 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Code:
$ ls ~/scripts/*  | egrep "[ ]"
my [ bracket ] file
filename is actually 'my [ bracket ] file'

ohh you want to remove [ ] and leave the rest using grep?
Code:
$ ls ~/scripts/*  | awk ' { gsub ( /[][]/, "" ) ;  print }'
my  bracket  file
your example
Code:
$ 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"}}

$ string2=$(echo -e "$string" | awk '{ gsub ( /[][]/, "") ; print}')
 
$ echo $string2
{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}}
your [ ] are gone.

Last edited by BW-userx; 02-12-2018 at 10:57 AM.
 
Old 02-12-2018, 10:50 AM   #3
harshagr
LQ Newbie
 
Registered: Feb 2018
Posts: 9

Original Poster
Rep: Reputation: Disabled
Does not work. It has to be something with awk to work.
 
Old 02-12-2018, 11:03 AM   #4
harshagr
LQ Newbie
 
Registered: Feb 2018
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
Code:
$ ls ~/scripts/*  | egrep "[ ]"
my [ bracket ] file
filename is actually 'my [ bracket ] file'

ohh you want to remove [ ] and leave the rest using grep?
Code:
$ ls ~/scripts/*  | awk ' { gsub ( /[][]/, "" ) ;  print }'
my  bracket  file
your example
Code:
$ 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"}}

$ string2=$(echo -e "$string" | awk '{ gsub ( /[][]/, "") ; print}')
 
$ echo $string2
{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}}
your [ ] are gone.

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]
 
Old 02-12-2018, 11:30 AM   #5
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
See the match function: https://www.gnu.org/software/gawk/ma...ring-Functions

Or using your awk script, you could explicitly print out the brackets - print "["$1"]"
 
Old 02-12-2018, 11:41 AM   #6
harshagr
LQ Newbie
 
Registered: Feb 2018
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by norobro View Post
See the match function: https://www.gnu.org/software/gawk/ma...ring-Functions

Or using your awk script, you could explicitly print out the brackets - print "["$1"]"
I tried all but it does not work, Can you please help with a exact command to get the desired output I am looking for.
 
Old 02-12-2018, 11:46 AM   #7
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
So this does not work?
Code:
awk 'NR>1{print "["$1"]"}' RS=[ FS=]
Post the commands that you tried.
 
1 members found this post helpful.
Old 02-12-2018, 11:53 AM   #8
harshagr
LQ Newbie
 
Registered: Feb 2018
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by norobro View Post
So this does not work?
Code:
awk 'NR>1{print "["$1"]"}' RS=[ FS=]
Post the commands that you tried.
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]
 
Old 02-12-2018, 12:03 PM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Code:
userx@slackwhere:~
$ echo -e $string | awk {'print "["$1"]"'}
[{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]
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 ]

Last edited by BW-userx; 02-12-2018 at 12:08 PM.
 
Old 02-12-2018, 03:28 PM   #10
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
I obtained good results with all of these ...
Code:
echo
echo; echo "Method #1 of LQ member danielbmartin."
awk 'BEGIN{FS="[][]"} {print "["$2"]"}' $InFile >$OutFile
echo "OutFile ..."; cat $OutFile; echo "End Of OutFile ($(wc -l <$OutFile) lines)"

echo
echo; echo "Method #2 of LQ member danielbmartin."
awk -F"[][]" '{print "["$2"]"}' $InFile >$OutFile
echo "OutFile ..."; cat $OutFile; echo "End Of OutFile ($(wc -l <$OutFile) lines)"

echo
echo; echo "Method #3 of LQ member danielbmartin."
 cut -d[ -f2 $InFile   \
|cut -d] -f1           \
|sed 's/\(.*\)/[\1]/'  \
>$OutFile
echo "OutFile ..."; cat $OutFile; echo "End Of OutFile ($(wc -l <$OutFile) lines)"

echo
echo; echo "Method #4 of LQ member danielbmartin."
sed 's/\(.*\[\)\(.*\]\)\(.*$\)/[\2/' $InFile >$OutFile
echo "OutFile ..."; cat $OutFile; echo "End Of OutFile ($(wc -l <$OutFile) lines)"
Daniel B. Martin

.
 
Old 02-12-2018, 07:24 PM   #11
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
I assume the homework deadline has passed so here is a version with the awk match function:
Code:
awk '{ match($0, /(\[.*\])/, capture)} {print capture[1]}' input_file
@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.
 
Old 02-12-2018, 08:17 PM   #12
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Code:
egrep -o '\[[^]]*]' file
 
Old 02-12-2018, 09:09 PM   #13
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by keefaz View Post
Code:
egrep -o '\[[^]]*]' file
Ding ding ding ding ding! We have a winner! keefaz gets the Blue Ribbon for brevity and elegance!!

Daniel B. Martin

.
 
Old 02-12-2018, 09:39 PM   #14
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by norobro View Post
@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
 
1 members found this post helpful.
Old 02-13-2018, 03:47 AM   #15
harshagr
LQ Newbie
 
Registered: Feb 2018
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by norobro View Post
So this does not work?
Code:
awk 'NR>1{print "["$1"]"}' RS=[ FS=]
Post the commands that you tried.
It worked for me. Thanks a Ton.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
split comma and square brackets in csv file curosity Programming 16 10-08-2012 09:28 AM
zsh completion and square brackets Stigius Linux - Software 0 06-27-2010 09:39 AM
Capture data in brackets nonnumquam Programming 11 07-11-2009 10:31 PM
Two square brackets in bash condition tirengarfio Programming 1 07-07-2009 12:36 PM
Konqueror and Square Brackets in Folder/File Names fortezza Linux - Software 2 12-19-2005 10:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration