Technology Sharing

How can adb shell logcat -b all|grep grep two substrings?

2024-07-06

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

existadb shell logcatCombine in commandgrepTo filter logs, if you want to match two substrings at the same time, you can use a pipe (|) Put twogrepCommands are linked together, or usinggrepof-E(oregrep, which is equivalent to-E) option to support extended regular expressions, so you can use logical OR (|) to match multiple patterns.

Method 1: Use twogrepOrder

In this method, the firstgrepThe command filters out the lines containing the first substring, then the secondgrepThe command then filters out the lines containing the second substring from these lines.

 

bash复制代码

adb shell logcat -b all | grep '子串1' | grep '子串2'

Method 2: Usegrepof-EOptions

This approach is more concise because it allows you togrepMultiple modes can be specified in the command at the same time.

 

bash复制代码

adb shell logcat -b all | grep -E '子串1|子串2'

But please note that the above command will actually match lines containing either "substring 1" or "substring 2", not lines containing both. If you want to match both substrings at the same time, you should write:

 

bash复制代码

adb shell logcat -b all | grep '子串1' | grep '子串2'

Or usegrepof-z(Note: This is not a standardgrepbut some versions ofgreporzgrepSupport) orawketc. to handle multi-line matching (which is often more complex and not necessary for simple scenarios).

However, if you really need to match two substrings in a regular expression (and the two substrings can appear anywhere on the same line), you can use the analogy of logical AND (although you are actually using logical OR).|, but by ensuring that both substrings appear in the pattern):

 

bash复制代码

adb shell logcat -b all | grep -E '子串1.*子串2|子串2.*子串1'

This command will match the occurrence of "substring 1" first and "substring 2" second, or "substring 2" first and "substring 1" second in the same line. Note that this pattern does not strictly require the two substrings to be adjacent, and there can be any number of characters between them. If you need them to be adjacent, you can remove the middle.*