SoFunction
Updated on 2025-03-02

Linux search log cat and grep

linux search log cat and grep

1. Query the 30 lines before and after the keyword

cat Log file name.log | grep -30 'Keywords'
  • example:
cat  | grep -30 '2019-04-08 13:30:04,271'

2. Query two keywords appear at the same time

cat Log file name.log | grep 'Keyword 1' | grep 'Keyword 2'
  • example:
cat  | grep 'ServiceV5Impl' | grep 'Customer name cannot be empty'

3. Compressed file query

gzip -dc file name | grep -30 “Keywords”
  • example:
gzip -dc mcs-all.log_2019 | grep -30 '2019-04-04 17:38:26,036'

4. The last item of the matching pattern in the query

cat file name | grep ‘Keywords' | tail -n
  • example:
cat mcs-all.log_2019-04-09 | grep ‘Customer name cannot be empty' | tail -1
 cat mcs-all.log_2019-04-09 | grep 'Customer name cannot be empty' | tail -100

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.