I am trying to get some specific text from logcat. It could be repeating multiple times. Example content would be:
..., stateEventTime=2025-01-09T17:22:05.635Z)], specificword=[SpecificWord(displayTimestamp=2024-06-01T00:00:00Z, LanguageCode=US English, ScreenId=ABC1-1, EventTime=2025-01-29T19:20:16.521Z)], status=status(more....
The problem is when I grep specificword, I am given too much json data to easily visually parse, as I only care about what is within specificword. Note, the brackets could be empty, such as:
specificword=[]
I would love to do the following:
adb logcat | grep my.app | command
And received something like the following:
specificword=[SpecificWord(displayTimestamp=2024-06-01T00:00:00Z, LanguageCode=US English, ScreenId=ABC0-0, EventTime=2025-01-29T19:20:16.521Z)]
specificword=[]
Thanks for the help!
I am trying to get some specific text from logcat. It could be repeating multiple times. Example content would be:
..., stateEventTime=2025-01-09T17:22:05.635Z)], specificword=[SpecificWord(displayTimestamp=2024-06-01T00:00:00Z, LanguageCode=US English, ScreenId=ABC1-1, EventTime=2025-01-29T19:20:16.521Z)], status=status(more....
The problem is when I grep specificword, I am given too much json data to easily visually parse, as I only care about what is within specificword. Note, the brackets could be empty, such as:
specificword=[]
I would love to do the following:
adb logcat | grep my.app | command
And received something like the following:
specificword=[SpecificWord(displayTimestamp=2024-06-01T00:00:00Z, LanguageCode=US English, ScreenId=ABC0-0, EventTime=2025-01-29T19:20:16.521Z)]
specificword=[]
Thanks for the help!
Assuming:
grep
supports -P
(PCRE) option. [important]Then would you please try:
grep -Po 'specificword=(\[(?>[^][]|(?1))*\])' test.txt
test.txt:
stateEventTime=2025-01-09T17:22:05.635Z)], specificword=[SpecificWord(displayTimestamp=2024-06-01T00:00:00Z, LanguageCode=US English, ScreenId=ABC0-0, EventTime=2025-01-29T19:20:16.521Z)], specificword=[], foo[](bar,baz), specificword=[this is some(of the possible[text]), including commas and paired [square] brackets]
Output:
specificword=[SpecificWord(displayTimestamp=2024-06-01T00:00:00Z, LanguageCode=US English, ScreenId=ABC0-0, EventTime=2025-01-29T19:20:16.521Z)]
specificword=[]
specificword=[this is some(of the possible[text]), including commas and paired [square] brackets]
[Explanation of the regex]
specificword=(regex)
matches the literal string followed by the regex
to be the 1st capture group.\[ ... \]
specifies the pattern to be surrounded by square brackets.(?> ... )
configures an atomic group to avoid unnecessary backtracking.[^][]
matches any character other than square brackets.(?1)
tells the regex engine to apply the regex surrounded by the 1st
(outermost) parentheses at the current position. This enables the recursion
to match balanced constructs.(?>[^][]|(?1))*
matches 0 or more sequences of alternation which matches
[^][]
or (?1)
.
grep 'specificword=[^,]*'
? – Cyrus Commented Feb 4 at 1:25[this is some(of the possible[text])]
contain comma? If so @Cyrus's suggestion won't work. Can it contain unmatched or somehow-escaped]
s? etc... The right answer will depend on what that string can/can't contain and, since you said "I am given too much json data...", if the input is JSON you should consider using a JSON parser likejq
to operate on it, notgrep
. – Ed Morton Commented Feb 4 at 14:44specificword=[]
at all, which is a common occurance, there are plenty of other commas and brackets outside of the text we care about. I believe all the brackets are matched and not escaped. – Brett Commented Feb 4 at 18:07adb logcat
and your desired output (no description) for that sample input to your question. – Cyrus Commented Feb 4 at 21:26[
and]
within the text you want to match. We need something we can copy/paste to test with, not just 1 line of input with some relevant characteristics (i.e. commas) and then a block of lines of other potentially problematic input. Provide a minimal reproducible example - a few lines of input covering all your use cases and the expected output given that input. – Ed Morton Commented Feb 5 at 13:39