I have a below mentioned Json file. And i need to parse this file with an input argument that i will pass to my shell script. So Basically i need to parse the values which are inside "collection" from the below JSON file. So if i will pass Janan as a input argument then i need Janan_2 as output or if i pass Janan_ex1 then i need Janan_loc_data as output
Input: abc.txt
{"znode":{
"path":"/aliases.json","prop":{
"ver":23,
"aver":10,
"count":0,
"time":"Sat Mar 07 04:39:53 GMT 2015 (1425703193598)",
"cver":0,
"czxid":123,
"Owner":0,
"mtime":"Tue Sep 22 05:59:16 GMT 2015 (1442901556990)",
"mzxid":123,
"pzxid":463856470714,
"dataLength":272},
"data":"{\"collection\":{\n \"Janan\":\"Janan_2\",\n \"Janan_ex1\":\"Janan_loc_data\",\n \"Neha\":\"Neha_1\",\n \"cric\":\"cric_2\",\n \"San\":\"San_1\",\n \"Arp\":\"Arp_1\",\n \"Nipun_test4\":\"Nipun_test3\",\n \"tran_Nipun\":\"tran_Nipun_2\",\n \"Zing\":\"Zing_1\"}}"},"tree":[{"data":{
"title":"/aliases.json","attr":{
"href":"zookeeper?detail=true&path=%2Faliases.json"}}}]}
So actually issue is that i am not able to fetch the values when i am using my command inside my shell and the same command is working fine while running in command line.
Below command is working fine while running at command line
cat abc.txt | awk -v RS=',?\\\\n[[:space:]]+' 'gsub(/\\"/,"")' | cut -d"{" -f3,3 | cut -d"}" -f1,1 | grep Janan_ex1 | cut -d":" -f2,2
output from above is : Janan_loc_data
But when i am using it inside my shell script, with the below arguments
INPUT=Janan_ex1
alias_1="user/arpan/abc.txt"
alias_2=`cat $alias_1 | awk -v RS=',?\\\\n[[:space:]]+' 'gsub(/\\"/,"")' | cut -d"{" -f3,3 | cut -d"}" -f1,1 | grep $INPUT | cut -d":" -f2,2`
OUTPUT:-
\atm_2\,\n \atm_ex1\
This is because it is not able to read special character \ inside shell scripts
For below command when using inside the script,
alias_2=`cat $alias_1 | awk -v RS=',?\\\\n[[:space:]]+' 'gsub(/\\"/,"")' | cut -d"{" -f3,3 | cut -d"}" -f1,1 | grep $INPUT`
I am getting below output:-
\n \Janan\:\Janan_2\,\n \Janan_ex1\:\Janan_loc_data\,\n \dps\:\dps_1\,\n \ammf\:\ammf_2\,\n \mob_search\:\mob_search_1\,\n \dnb\:\dnb_1\,\n \stamping_test4\:\stamping_test3\,\n \tran_stamping\:\tran_stamping_2\,\n \mrch_generic\:\mrch_generic_1\
Also i cannot use json parser inside my script as it is not installed in machine
Details regarding my unix flavor
$ uname -a
Linux 2.6.32-573.3.1.el6.x86_64 #1 SMP Mon Aug 10 09:44:54 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
jq).awkwas designed for line-oriented text, not structured input like JSON.grep collection abc.txt | sed -e 's/[[:space:]]\\"/\n/g' -e 's/}}"},.*//' -e 's/\\",\\n//g' -e 's/\\"//g'and see if you can use the output for the translation.