3

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.

2 Answers2

1

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.

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
0

With the sample input you posted this is all you need:

$ awk -F\" '/end/{print t} {t=$2}' file
Name

If that's not adequate then come up with more truly representative sample input that includes the cases you think will be difficult for a script to deal with. It's always trivial to extract the text you want and MUCH harder to not extract text you don't want.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185