Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm looking to refactor the following command:

tail -f production.log |
    grep -e "Processing " -e "compatible;" -e "Completed in " -e

This is the output of the command:

Processing BasketController#cross_sell_product (for 85.189.237.x at 2011-08-03 12:46:07) [POST] "RequestLog","2011-08-03 12:46:07","12595","www.website.com","/basket/cross_sell_product/113","85.189.237.x","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)" Completed in 0.07853 (12 reqs/sec) | Rendering: 0.01402 (17%) | DB: 0.01439 (18%) | 200 OK [https://www.website.com/basket/cross_sell_product/113]

This is great and exactly what I want however I'd like to see is a carridge return or two afterwards so I can study the logs as blocks.

I tried to add \n to the end however this didn't work.

share|improve this question
1  
Do you mean a newline after each match, or after the whole result? –  rozcietrzewiacz Aug 3 '11 at 11:56
    
Carriage return is "\r". You want them where/after what exactly? Do you want to have a few blank lines between every line of output? –  jw013 Aug 3 '11 at 12:01
    
Yes thats exactly what I want a few blank lines between every line of output? –  user9563 Aug 3 '11 at 12:05
add comment

2 Answers

grep will filter out empty lines since they don't match any pattern. In the case empty line separators appear in the original file you can let them pass through, adding -e "^$".

If those empty lines are not present in the original file, you'll have to add them. This is an example of how to do it:

 tail -f production.log | grep -e … | sed 's/\(^Processing \)/\n\1/'

Edit: if you want an empty line after every other line use sed 's/$/\n/' instead. And of course you can add as many \n as you like.

share|improve this answer
add comment

I'm not sure what you want, but awk can probably do it easily. To add an extra newline after each matching line:

tail -f production.log |
awk '/Processing|compatible;|Completed in / {print; print ""}'

To add a newline between all blocks of non-consecutive matching lines:

tail -f production.log |
awk '
/Processing|compatible;|Completed in / {
    if (NR == n+1) print "";
    n = NR;
    print;
}'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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