Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep the adb logcat & write the output to a text file

I want to grep the adb logcat & write the output to a text file. If I just do

./adb logcat > std.txt 

it writes the entire log to the text file & If I do

./adb logcat | grep ABC 

it prints all lines containing ABC to my terminal. But now I wish to search for ABC & write only these lines to a text file.

./adb logcat | grep ABC > std.txt 

doesn't work. Plz help.

like image 555
AndroidGuy Avatar asked Feb 05 '13 06:02

AndroidGuy


People also ask

How do I get Logcat logs?

View your app logs To display the log messages for an app: Build and run your app on a device. Click View > Tool Windows > Logcat (or click Logcat in the tool window bar).

What is Logcat adb?

Logcat is a command-line tool that dumps a log of system messages, including stack traces when the device throws an error and messages that you have written from your app with the Log class. This page is about the command-line logcat tool, but you can also view log messages from the Logcat window in Android Studio.

Where is adb Logcat stored?

They are stored as circular memory buffers on the device. If you run "adb logcat > myfile" on your host system, you can retrieve the content into a file.


2 Answers

I think there is a problem with grep buffering. You can try something like this:

./adb logcat | grep --line-buffered ABC > std.txt 

It should be the same problem for chained grep.

EDIT: A similar question can be found here: Why no output is shown when using grep twice?.

like image 60
Brtle Avatar answered Sep 29 '22 16:09

Brtle


Edit: This seems to work

 ./adb logcat |grep --line-buffered  ABC >a.txt 

I can explain you what is happening heres. Hope someone can derive a solution from that.If you run the following command in terminal

cat |grep "ABC" 

and start entering lines of text, you can see that grep immediately outputs any lines that contains.

 cat somefile.txt | grep "ABC"  

will print all lines that contains 'ABC' to the terminal, as expected.

But if you run

cat |grep ABC >a.txt 

and start entering text on the terminal, you can see that the file is not written until you enter an EOF character (Ctrl+ D) and make cat terminate.

But using --line-buffered gives output in the expected way

cat |grep --line-buffered  ABC >a.txt 
like image 27
Vishnuprasad R Avatar answered Sep 29 '22 15:09

Vishnuprasad R