40

I want to grep the adb logcat and 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.

This:

./adb logcat | grep ABC > std.txt

doesn't work.

What's the correct command?

Jeff Schaller
  • 2,352
  • 5
  • 23
  • 38
AndroidGuy
  • 1,270
  • 4
  • 15
  • 32
  • I see the same problem. If I do `adb logcat | grep ABC` which produces among other things the string "XYZ", and then do a `adb logcat | grep ABC | grep XYZ` I get nothing. And I am absolute sure that output lines are produces which contains both "ABD" and "XYZ". How is this? – Bjarke Freund-Hansen Aug 09 '13 at 10:00
  • Which host OS, which grep? Works e.g. on my OS X with the supplied BSD grep – laalto Aug 09 '13 at 12:18
  • If you used eclipse you could use the special "save" button which does exactly this... :P –  Aug 15 '13 at 11:06
  • Hey, check my answer. It is a working solution! :) – Levente Kurusa Aug 16 '13 at 06:08
  • 1
    Thanks for all the great answers. It makes perfect sense, grep is buffering quite a lot of output from logcat, before it is returned. So whatever is after the grep in the pipe, will only receive output when some larger amount of data are ready to be emitted from grep. But by line buffering it in grep, grep will emit each line as it is filtered forward in the pipe. – Bjarke Freund-Hansen Aug 16 '13 at 11:09

8 Answers8

58

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?.

Community
  • 1
  • 1
Brtle
  • 2,297
  • 1
  • 19
  • 26
  • I'm not sure how I got by not knowing about "--line-buffered". This answer deserves a whole lot more upvotes! – Peaker Nov 05 '13 at 13:05
6

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
Vishnuprasad R
  • 1,682
  • 13
  • 23
5

This is working for me:

./adb logcat | grep ABC | dd of=/home/levex/dump.txt

Explanation: ./adb logcat opens the logcat, and then it goes through a pipe to grep ABC which filters the lines to those containing ABC, and then again through a pipe to dd of=/home/levex/dump.txt to finally print it to a file. of=xxx parameter sets the output file.

Levente Kurusa
  • 1,858
  • 14
  • 18
3

Try adb logcat | grep ABC | tee out

pierrotlefou
  • 39,805
  • 37
  • 135
  • 175
1

Try this

./adb logcat -s "ABC" > std.txt
Zombo
  • 1
  • 62
  • 391
  • 407
UdayaLakmal
  • 4,035
  • 4
  • 29
  • 40
  • Sorry UdayaLakmal & svnpenn, but this isn't working. It just writes these to lines to the file: --------- beginning of /dev/log/system --------- beginning of /dev/log/main – AndroidGuy Feb 05 '13 at 07:06
  • Did you then run the application which produce "ABC" string, i tested this and following is my result for string "drriveAPI" adb server is out of date. killing... * daemon started successfully * --------- beginning of /dev/log/main --------- beginning of /dev/log/system D/drriveAPI(27329): http://d.com/tag/abcdef/assign? D/drriveAPI(27329): URLhttp://d.com/tag/abcdef/assign? D/drriveAPI(27329): http://d.com/tag/abcdef/assign? D/drriveAPI(27329): URLhttp://d.com/tag/abcdef/assign? – UdayaLakmal Feb 05 '13 at 07:17
1

Something like that maybe looking for all entries with word time, testapk1 and testapk2

adb logcat -v time testapk1 testapk2 *:S -d > adblogfilter.log 
1

Save LogCat To A Text File

"To save LogCat to a text file open up a terminal window and type: adb logcat -d > logcat.txt"

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Adnama
  • 172
  • 2
  • 12
0

try this

adb logcat -d -e "ABC" > std.txt
El David
  • 616
  • 8
  • 17