3

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.

3
  • 1
    Do you mean a newline after each match, or after the whole result? Commented Aug 3, 2011 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? Commented Aug 3, 2011 at 12:01
  • Yes thats exactly what I want a few blank lines between every line of output? Commented Aug 3, 2011 at 12:05

2 Answers 2

3

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;
}'
1

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.