1

When I do grep on a bunch of files, and several file have the word I am looking for, the output is hard to read as it's one after the other. Is there anyway to insert additional \n's after the matching output of each file.

Example, current output what I get

$ grep word daily_log.201407*
<daily_log.20140702-matches  about 100 lines>
<daily_log.20140704-matches  about 10 lines>
<daily_log.20140706-matches  about 50 lines>

Trying to achieve something like

$ grep word daily_log.201407*
<daily_log.20140702-matches  about 100 lines>


<daily_log.20140704-matches  about 10 lines>


<daily_log.20140706-matches  about 50 lines>

Hope the question is clear. Any way to do this?

2

You can grep each file and add extra lines based on the result of the grep (ie. not for files that did not match):

for fn in daily_log.201407* ; do
grep word "$fn"
if [ $? == 0 ] ; then
   echo -e '\n\n\n'
fi
done
  • @UlrichDangel It adds one after each line, maybe I am missing what the OP wants, his example has a single matching line. – Anthon Jul 16 '14 at 7:24
  • Based on the description, my understanding was that OP wants to have the grep output delimited by newlines after each file but now i am no longer sure – Ulrich Dangel Jul 16 '14 at 7:27
  • @UlrichDangel You are probably right, but I would have given a less ambiguous example in his case, using multiple lines and then some ellipses. I updated my answer to reflect that interpretation. – Anthon Jul 16 '14 at 7:46
1

The easiest way to do this is likely to be going to awk instead:

awk 'FNR==1 { print "\n\n\n" } ; /word/ { print FILENAME ":" $0 }' daily_log.201407*

That prints out every line matching word with the filename before it (like grep does with multiple files). Before the first line of every file it prints a few blank lines. It knows it's at the start of the file because FNR (the number of the line in the current file) is 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.