Getting a short string from a long one without spaces

I have a file with a huge string without spaces in it (something like: "A":123,"B":456,"C":789…)

I want to get X amount of chars before and after a specific value (like what is around "B").

But, if I use cat, because the string doesn’t have spaces, the whole string returns.

I tried cat time_series.json | grep "B" | cut -c1-50 but again the whole returned.

I tried cat time_series.json | grep "B" | cut -b 1-400 but it only returned the start (and my string is in the middle)

What can I do?

Asked By: Ran


You can ask grep to only output matching patterns, and include wildcards for the appropriate number of characters around the marker you’re looking for; e.g.

grep -E -o '.{50}"B".{50}'

to show "B" with 100 characters of context.

Answered By: Stephen Kitt
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.