In putty, grep command is widely used to search for a text in a large log or text files for debugging purposes. By default, grep displays the matching lines.
1) Search For a String in a File
grep "User Name is" output16042014.log
2) Search for a String in multiple files
grep "User Name is" output*.log
3) Search for a case insensitive String in a file
grep -i "User Name is" output*.log
4) Search for strings having full words in a file
grep -iw "use" output16042014.log
5) Display lines after a String match in a file
grep -A 20 "User Name is" output16042014.log
This will print 20 lines after a line containing "User Name is"
6) Display lines before a String match in a file
grep -B 20 "User Name is" output16042014.log
This will print 20 lines previous to line containing "User Name is"
7) Highlighting the search using GREP_OPTIONS
export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
grep "User Name is" output16042014.log
8) Search for a string recursively
grep -r "User Name is" output16042014.log
Search recursively for a string in all the files.
9) Counting the number of matches using grep -c
grep -c "User Name is" output16042014.log
No comments:
Post a Comment