0

I have an input text file (shown below) which I have to call in a shell script and also in shell script I will pass an input argument for which I have find a corresponding value from the input text file.

So if my input argument is Janan I have to return Janan_2 as output, or if my input is Janan_ex1 then output will be Janan_loc_data.

Input:

"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\"}}"}
6
  • Hi Cyrus ..this link seems more on HTML tag and stuff .. I need some help in Shell scripting..which i am not much familiar with specially AWK Commented Sep 24, 2015 at 6:07
  • 4
    If you're parsing JSON data then use jq in unix shells Commented Sep 24, 2015 at 6:10
  • Anubhava can you please share an example if you have something handy Commented Sep 24, 2015 at 6:12
  • 1
    Is your input data really on one line? Does it really contain all the backslashes? Is it really JSON-encoded data, with the collection as a string value instead of a regular JSON object enclosed in { … }? That's just brutal. Commented Sep 24, 2015 at 6:13
  • Jonathan ..i am getting this line with grep command and I get this file from some other department in exact same format. Commented Sep 24, 2015 at 6:20

1 Answer 1

1

Pipe your input "file" into the following bash script with your "input argument" as the argument, e.g. ...|./bashscript San:

#!/usr/bin/bash

sed 's/[^ ]*// ; s/}}"}// ; s/"://g ; s/,\\n/\n/g'|
tr -d '\\'|
awk -F\" '$2=="'$1'"{print $3}'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.