tail -f ... | What if the log is not one line when tracking the log with grep?

Asked 1 years ago, Updated 1 years ago, 100 views

tail -f mylog.log | grep some-keyword

It is possible to filter the log by pipe + grep in the same way as above The problem is that this method does not filter the output of many lines, not one line, and everything except the first line is cut.

 The first line blah blah
Second line, some-keyword blah blah
Third line related log1
Log related to the fourth line 2
Log 3 related to line 5
Sixth row, blah blah
Seventh line blah blah

Is there a way to filter the second to fifth lines when the log is printed like above?

unix linux terminal shell grep

2022-09-22 13:11

1 Answers

Assuming that the logs are logged regularly, I think we can take advantage of option -A.

The option to output up to a few lines after that keyword.

tail -f mylog.log | grep -A3 some-keyword


2022-09-22 13:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.