1

I have a file, say test.txt which looks like a json and it contains a single line of data:

{"a":"b"},{"c":"d, e, f"},{..} and so on

I need to save all the data within each pair of braces in a different file in separate lines.

For eg: result.txt having

"a":"b"
"c":"d, e, f"

I used awk with regex in bash scripting

awk '/\{(.*?)\}/' test.txt  > result.txt

But instead of cropping up individual braces, it's printing the exact test file as it is.

Can anyone say what's going wrong?

2
  • 1
    looks like a json or an actual JSON? Commented Jan 12, 2017 at 9:12
  • It is actually a json response to a query to extract the properties of a certain page. I saved it in the txt file to sort out the values. I know I should use a json parser but I tried doing it this way. Commented Jan 12, 2017 at 9:52

2 Answers 2

1
echo '{"a":"b"},{"c":"d e f"},{..}' |awk -v RS="{|}" '{gsub(/,/,"")} /./{print $0}'
"a":"b"
"c":"d e f"
..

Note: It's a good idea to use json editing tools like jq to play around with json files.

Sign up to request clarification or add additional context in comments.

5 Comments

It does work on the console. But when I try to assign the result to a file, the results don't seem to appear in new lines.
Its working for me . try echo bla bla bla | grep -oP '{\K.*?[^}]+' as alternate solution
@Kritzii: That is the exactly the problem of using text processing tools which rely on regEx in one way or the other, the slightest change in JSON formatting like an extra space will screw everything up
In total agreement with Inian. however, I am running over old Linux box which do not have jq and cannot get it due to some management reasons (not is my control ) then people have to search for alternatives. Reiterating , getting jq is best solution if possible.
Thanks @PS , the code worked. I have used Jackson parser before. I just tried working with awk as I am new to scripting.
0

Or, you can use grep with lookarounds to capture only the value between curly braces:

grep -oP '(?<={).*?(?=})' test.txt > result.txt

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.