LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-13-2018, 08:06 AM   #16
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757

Quote:
Originally Posted by danielbmartin View Post

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

.
You can also write the regexp like
Code:
awk -F"[\\\[\\\]]" '{print "["$2"]"}' file
May be more understandable but it sure looks ugly
 
1 members found this post helpful.
Old 02-14-2018, 09:40 PM   #17
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,675

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
Code:
"[[]]"
 [ character set starts
 [ literal character in the set
 ] character set ends
 ] literal character

"[][]"
 [ character set starts
 ] literal character in the set (a character set cannot be empty)
 [ literal character in the set
 ] character set ends
Code:
"[\\\[\\\]]"
can be written as
Code:
'[\[\]]'
because in the shell the \ is a literal character within a 'string'
Code:
echo 'a \ is a \'
 
Old 02-14-2018, 10:14 PM   #18
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 630

Rep: Reputation: 315Reputation: 315Reputation: 315Reputation: 315
OP seems to have accepted an answer,
Providing a possible alternative, using any language that parses json can make handling this much easier.

In the example output, OP appears to be looking for the information in a dictionary under the key command1, which has another dictionary in it. In that dictionary, looking for the "response" key, which has a list of information.
You can say that with something like data["command1"]["response"]
For the example output (correcting the mis formatting -> [{"eventdate":"2018-04-06","facility_fee":null]}) using python

Code:
import json

response = '''{
    "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"
            }
    }
}'''
 
data = json.loads(response)
print(data['command1']['response'])
Code:
[{'eventdate': '2018-04-06', 'facility_fee': None}]
 
Old 02-14-2018, 10:27 PM   #19
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,814

Rep: Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639
Quote:
Originally Posted by MadeInGermany View Post
Code:
"[\\\[\\\]]"
can be written as
Code:
'[\[\]]'
This didn't work on my machine.

This code ...
Code:
echo
echo; echo "Method #2 of LQ Guru keefaz."
awk -F"[\\\[\\\]]" '{print "["$2"]"}' $InFile >$OutFile
echo "OutFile ..."; cat $OutFile; echo "End Of OutFile ($(wc -l <$OutFile) lines)"

echo
echo; echo "Method #1 of LQ Member MadeInGermany."
awk -F'[\[\]]' '{print "["$2"]"}' $InFile >$OutFile
echo "OutFile ..."; cat $OutFile; echo "End Of OutFile ($(wc -l <$OutFile) lines)"
... produced this result ...
Code:
Method #2 of LQ Guru keefaz.
OutFile ...
[{"eventdate":"2018-04-06","facility_fee":null]
[{"eventdate":"2018-04-08","facility_fee":null]
[{"eventdate":"2018-04-10","facility_fee":null]
End Of OutFile (3 lines)


Method #1 of LQ Guru MadeInGermany.
awk: warning: escape sequence `\[' treated as plain `['
awk: warning: escape sequence `\]' treated as plain `]'
OutFile ...
[]
[]
[]
End Of OutFile (3 lines)
Daniel B. Martin

.
 
Old 02-15-2018, 03:35 AM   #20
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,675

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
You are right (and I was wrong), in
Code:
"[\\\[\\\]]"
the shell substitutes \\ but not \]
So the equivalent is
Code:
'[\\[\\]]'
 
1 members found this post helpful.
Old 02-15-2018, 07:23 AM   #21
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,814

Rep: Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639
Quote:
Originally Posted by MadeInGermany View Post
... the equivalent is
Code:
'[\\[\\]]'
Good. Thanks for the "fix." All is well.

Daniel B. Martin

.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 03:35 AM.

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