android - Command line getting occurrences of text within brackets following a specificword= - Stack Overflow

admin2025-04-16  4

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!

Share Improve this question edited Feb 6 at 19:33 Brett asked Feb 3 at 22:21 BrettBrett 3621 gold badge3 silver badges12 bronze badges 7
  • 1 grep 'specificword=[^,]*'? – Cyrus Commented Feb 4 at 1:25
  • 1 Can the real text that can appear where your example has [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 like jq to operate on it, not grep. – Ed Morton Commented Feb 4 at 14:44
  • The recommendation does not handle specificword=[] 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:07
  • 1 Add output of adb logcat and your desired output (no description) for that sample input to your question. – Cyrus Commented Feb 4 at 21:26
  • 1 Please edit your question to provide sample input and expected output that includes [ 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
 |  Show 2 more comments

1 Answer 1

Reset to default 1

Assuming:

  • The text of intetest may contain commas and matched (and possibly nested) square brackets.
  • The text may be empty.
  • Your 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).
转载请注明原文地址:http://www.anycun.com/QandA/1744747154a87037.html