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.
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).
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.
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.
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?.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With