GREP - A Practical Guide ๐Ÿš€

ยท6 min read
GREP - A Practical Guide ๐Ÿš€

Grep is a terminal command that allows users to search for specific patterns within text files and directories.


Problem: As a developer, you need to efficiently search through log files to find specific API requests or errors, but manually scanning through the logs is time-consuming and error-prone.

Solution: Utilize the grep command in the terminal to search for API requests within log files.

WHO'S THIS FOR

  • Anyone seeking to learn and use terminals more effectively.

Examples are based on API's logs mostly but can be used with any file. If you want to go throw all commands live - Git Clone Terminal/Grep

#Basics of Grep ๐Ÿ“

The basic syntax for grep is:

grep "pattern" file 

where: pattern is the regular expression you want to search for file is the name of the file you want to search

Grep works on all Unix-like systems.

Grep will print each line in the file that matches the regular expression.

By default, grep is case-sensitive, so "gnu" is different from "GNU" or "Gnu." You can make it ignore capitalization with the --ignore-case option.

1. Search for a pattern within a file

# grep "search pattern" path/to/file
grep "error" api_server.log
Grep search in file

2. Search stdin for lines that do match a pattern

Many times we want to pipe Grep with another command.

# cat path/to/file | grep "search_pattern"
cat api_server.log | grep error
Grep search with cat

3. Search pattern in multiple files in the current directory with .txt extension

cd logs
# grep "search pattern" *.txt
grep ERROR *.txt
Grep search in dir

Most Important Flags ๐Ÿšฉ

-i

Grep default is case-sensitive. Use this flag to make it search case-insensitive.

grep error api_server.log -i
Grep search case-insensitive

-v

Invert the match, print all lines where the pattern does not match.

grep INFO api_server.log -v
Grep search invert

-w

Search for the whole word. Sometimes there is a relative pattern match but we want an exact word. In that case, this flag is useful.

grep INFO api_server.log -w
Grep search without -w
Grep search with -w

-n

Show line numbers along with matching lines.

grep POST api_server.log -n
Grep search with line number

-l

Find file names that match the pattern.

#grep "pattern" *.ext -l 
grep ERROR *.txt -l
Return file name where pattern match

-R

If you only know the folder name and it contains subdirectories, you need to retrieve all file names and then search recursively within the directories.

grep ERROR -l -R
Recussive search

-o

Only print the matching part of the line (not the whole line)

grep "Internal Server Error" api_server.log -o
-o in Grep

-c

Let's say you have one deprecated API now you want to track how many users still use it throw logs. This flag will return the count.

grep "/api/v1/deprecated" api_server.log -c
# In multiple files
grep "/api/v1/deprecated" ./logs/*.txt -c
-c in Grep
-c in Grep in multiple files

-E

Interpret the pattern as an extended regular expression.

grep -E "user_id=[0-9]{4}" api_server.log
-c in Grep

Line Context Search ๐Ÿ”

-A: (Lines Above)

To display the line containing the error and the line directly preceding it, you can use -A 1: Example:

-A in Grep

-B: (Lines Below)

Continuing from the previous example, to display the line containing the error and the line directly following it, you can use -B 1: Example:

-B in Grep

-C: (Lines Containing)

To display the line containing the error and the lines directly above and below it, you can use -C 1: Example:

-C in Grep

# Real Life Examples ๐Ÿ’ก

If you're not familiar with REGEX, I'll explain it next.

Codebase Exploration:

I know we have a vs-code search. But searching through the terminal creates a great impression ๐Ÿ˜Ž

grep -r "getUserById" ./

Parsing and Extracting Information

grep -o -E "User: (\w+) performed action: (\w+)" user_log.log

This command uses a regular expression to capture user names and their corresponding actions.

User: Alice performed action: login
User: Bob performed action: view_profile
User: Alice performed action: post_comment
User: Charlie performed action: login
User: Alice performed action: view_profile
User: Bob performed action: post_comment

Pipe with another command to extract data

docker ps | grep -oE '^[0-9a-f]+'

This will output a list of container IDs for all running Docker containers.

f9e5f041b25a
2ab9d3fc5f8e

# Advance REGEX Search ๐Ÿง 

Search for any four consecutive digits in api_server.log

grep -E "user_id=[0-9]{4}" api_server.log

Matching Words Starting with 'A' or 'B':

grep -E '\b[A-Ba-b]\w+\b' api_server.log
-C in Grep

Match either/or

grep '400\|500' api_server.log|
-C in Grep

Bonus Tip โœจ

Ripgrep is much faster when you have long files.

# Install
sudo apt-get install ripgrep
#or
brew install ripgrep
# Syntax 
rg <search_pattern> <filename>
 

Conclusion

In conclusion, grep is a powerful tool that enables users to search, filter, and manipulate text data efficiently from the command line. Mastering grep can significantly enhance productivity and streamline text processing tasks in the terminal environment.

Happy Coding ๐Ÿ‘ฉโ€๐Ÿ’ป