Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract specific text from JSON using BASH

Tags:

json

bash

sed

I have copy-pasted the JSON structure into a .txt file for my input file (I will have a number of these).

I'm looking for the "Name" text between "start" and "end", my input file is large and the parts I want to extract from look like this:

Here: "start",
        title: "Name",
        type: "end",
        words: "more words",

I've tried the following but it doesn't work.

input.txt < sed -n '/^start",$/,/^type: "end"$/p' data > output.txt

Thank you.

like image 248
user3597867 Avatar asked Aug 06 '26 07:08

user3597867


1 Answers

data.txt

...
Here: "start",
        title: "Name",
        type: "end",
        words: "more words",
...

Sed command:

 $ sed -n '/start",$/,/ *type: "end",$/p' data.txt

Output:

Here: "start",
        title: "Name",
        type: "end",

^ won't match the start line since there's a "Here:" before it and "end"$ won't match the end line since there's a comman after the last quotation mark.

like image 153
PSkocik Avatar answered Aug 07 '26 21:08

PSkocik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!