Add carriage return to output of `tail` while using `grep`
Solution 1
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;
}'
Solution 2
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.
Related videos on Youtube
user9563
Updated on September 18, 2022Comments
-
user9563 over 1 year
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.-
rozcietrzewiacz almost 13 yearsDo you mean a newline after each match, or after the whole result?
-
jw013 almost 13 yearsCarriage return is "\r". You want them where/after what exactly? Do you want to have a few blank lines between every line of output?
-
user9563 almost 13 yearsYes thats exactly what I want a few blank lines between every line of output?
-