Linux Log Search - ashwin-shetty/Documents-Wiki GitHub Wiki

1. Tail

Tail is a command which prints the last few number of lines

 # By Default tails last 10 lines 
    tail /logs/application.out

 # -f (follow) Prints extra lines on to console added to the file after it is opened.
    tail -f  /logs/application.out | tee filtered_output.txt

 #  Copy result to another file
    tail -f  /logs/application.out | tee filtered_output.txt

 # All the line which does not contain  /192.168.1.1
    tail -f /logs/app.log | grep -v "/192.168.1.1"

 #  All the line which does not contain  /192.168.1.1 or com.ashu.app or Employee
    tail -f /logs/app.log | grep -Ev "/192.168.1.1|com.ashu.app|Employee"

2. Basic Grep Commands

grep is a command-line utility for searching plain-text data sets for lines that match a regular expression

   # -iw for exact word , Returns lines with exact word 
    grep  -iw "Post_Code" /logs/application.out

   # Content with trailing first 10 and last 10 lines
    grep -B 10 -A 10 -iw "Post_Code" /logs/application.out

   # All Files in Directory 
   grep  -iw "Post_Code" /logs/*

   # All Files with same name in Directory   
    grep  -iw "Post_Code" /logs/application.*

   # All Files with same extension in Directory      
    grep  -iw "Post_Code" /logs/*.out

Search and Replace

    # Search for 'original-string' , exclude extension .log and .out and replace string with 'replace-string'
    grep -rl "orginal-string"  --exclude=\*.{log,out} | xargs sed -i 's/orginal-string/replaced-string/g'