Java @ Desk: Putty | Java at your desk

Putty grep unix tutorial

Putty grep unix tutorial

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

Putty view, edit and save file

Putty view, edit and save file

In this post, we will understand about the commands that are used to view, edit/modify and save the file using putty.

vi is the command that is used to view the file.

vi filename - This command opens the file in the putty window

Once opened following commands are used to modify and save the file

i to insert
a to append
x to delete
dd to delete a line
: to begin a command sequence
:w to save

:q to quit
:q! to quit without saving
:wq to save and quit

Putty grep Count String Occurrences or Number Of String Matches In File

Putty grep Count String Occurrences or Number Of String Matches In File

grep command is used to find and print the string from the log file. Consider a log file which prints a bunch of log lines containing "Saved User Details For User...". To get how many user got saved into the database, simply we can count the number of occurences of above string in a log file.

Command to achieve this :

grep -c "Saved User Details For User" output16042014.log

Putty grep Find String In File

Putty grep Find String In File

In a big log or text file for debugging purposes we may require to search for a particular text. For example, if there is an application in which multiple threads are running and we want to check for the log "Data Saved For User Id....." to check if the data is saved or not for users.

To achieve this, grep command is used

1) Search For String "User Name is"
2) Log file Name is output16042014.log

grep "User Name is" output16042014.log