I have a huge JSON output and I just need to delete everything except a small string in each line.
The string has the format
"title": "someServerName"
the "someServerName" (the section within quotes) can vary wildly.
The closest I've come is this:
:%s/\("title":\s"*"\)
But this just manages to delete
"title": "
The only thing I want left in each line is
"title": "someServerName"
EDIT to answer the posted question:
The Text I'm going to be working with will have a format similar to
{"_links": {"self": {"href": "/api/v2/servers/32", "title": "someServerName"},tons_of_other_json_crap_goes_here
All I want left at the end is:
"title": "someServerName"
It should be .* rather than * to match a group of any characters. This does the job:
%s/^.*\("title":\s".*"\).*$/\1/
Explanation of each part:
%s/ Substitute on each matching line.^.* Ignore any characters starting from beginning of line.\("title":\s".*"\) Capture the title and server name. ".*" will match any characters between quotes..*$ Ignore the rest of the line./\1/ The result of the substitution will be the first captured group. The group was captured by parentheses \(...\).This sounds like a job for grep.
:%!grep -o '"title":\s*"[^"]*"'
For more help with Vim's filtering see :h :range!.
See man grep for more information on the -o/--only-matching flag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With