Hashim Aziz
Hashim Aziz

Reputation: 6185

Why does grep -a output to console?

When doing grep -a 5 "SEARCHSTRING" FILE.txt, grep seems to print the searching of the file to the console in real-time, which I find both useful and cool. However, when simply doing grep "SEARCHSTRING" FILE.txt, or grep -i -n --color -C 5 "SEARCHSTRING" FILE.txt, I get the usual blinking cursor while it's processing the file.

Why does grep -a output to console while searching a file, and how can I replicate that behaviour without using the -a option?

Upvotes: 0

Views: 811

Answers (1)

matzeri
matzeri

Reputation: 8496

Please note that your multiple questions about grep are not about programming, and it seems you have not read carefully the manual of grep. From man grep I see:

  -a, --text
          Process a binary file as if it were text; this is equivalent  to
          the --binary-files=text option.

So it is NOT redirecting the output,and the default is still console. There is no option, that I am aware, that delays the output.

Additional note: grep -a 5 "SEARCHSTRING" FILE.txt means open inputs in binary mode and search for strings 5 in files SEARCHSTRING and File.txt; so you will have more matches than grep "SEARCHSTRING" FILE.txt where you are asking instead for SEARCHSTRING in File.txt . May be this is the reason why you see immediate output and delayed one ? In the first case you have likely much more matches. You can verify it with

grep -a 5 "SEARCHSTRING" FILE.txt |wc -l 
grep -i "SEARCHSTRING" FILE.txt |wc -l 

and compare the numbers of matches. wc -l counts the number of lines

Upvotes: 1

Related Questions