Comprehensive List of Grep Commands with Descriptions - Printable Version +- WildlandsTech (https://wildlandstech.com) +-- Forum: Programming (https://wildlandstech.com/forumdisplay.php?fid=3) +--- Forum: Batch & Shell Scripting (https://wildlandstech.com/forumdisplay.php?fid=42) +--- Thread: Comprehensive List of Grep Commands with Descriptions (/showthread.php?tid=147) |
Comprehensive List of Grep Commands with Descriptions - Sneakyone - 09-03-2024 Comprehensive List of Grep Commands with Descriptions **grep** (Global Regular Expression Print) is a command-line utility used for searching and filtering text in files or standard input using regular expressions. Below is a detailed list of grep commands, along with descriptions and examples. 1. Basic String Search Description: Searches for a specific string in a file and prints the matching lines. Code: grep "search_string" [file] Code: grep "error" /var/log/syslog 2. Case-Insensitive Search Description: Performs a case-insensitive search by ignoring the case of the search string. Code: grep -i "search_string" [file] Code: grep -i "error" /var/log/syslog 3. Search Recursively in Directories Description: Recursively searches through all files in a directory and its subdirectories. Code: grep -r "search_string" [directory] Code: grep -r "error" /var/log 4. Display Line Numbers with Matches Description: Displays the line numbers of the matching lines in the output. Code: grep -n "search_string" [file] Code: grep -n "error" /var/log/syslog 5. Invert Match (Show Non-Matching Lines) Description: Displays all lines that do not match the search string. Code: grep -v "search_string" [file] Code: grep -v "error" /var/log/syslog 6. Display Only Matching Parts of Lines Description: Displays only the parts of lines that match the search string. Code: grep -o "search_string" [file] Code: grep -o "error" /var/log/syslog 7. Count the Number of Matches Description: Counts and displays the number of lines that match the search string. Code: grep -c "search_string" [file] Code: grep -c "error" /var/log/syslog 8. Use Extended Regular Expressions (ERE) Description: Enables extended regular expressions for more advanced pattern matching. Code: grep -E "regex_pattern" [file] Code: grep -E "error|warning" /var/log/syslog 9. Search for Whole Words Only Description: Matches only whole words instead of substrings. Code: grep -w "search_string" [file] Code: grep -w "error" /var/log/syslog 10. Show Context Lines (Before and After Matches) Description: Displays a specified number of lines before and/or after each matching line. Code: grep -C [num] "search_string" [file] Code: grep -C 3 "error" /var/log/syslog 11. Suppress Output (Useful for Exit Status Only) Description: Suppresses all output, useful when you only need to know if a match exists (exit status). Code: grep -q "search_string" [file] Code: grep -q "error" /var/log/syslog 12. Print Filename with Matches Description: Displays the name of the file where matches are found, useful when searching multiple files. Code: grep -l "search_string" [files] Code: grep -l "error" /var/log/* 13. Search for Files That Do Not Contain a String Description: Displays the names of files that do not contain the search string. Code: grep -L "search_string" [files] Code: grep -L "error" /var/log/* 14. Search Compressed Files (Using zgrep) Description: Searches within compressed files, typically using `zgrep` which behaves like `grep` but works with gzip-compressed files. Code: zgrep "search_string" [compressed_file] Code: zgrep "error" /var/log/syslog.gz 15. Highlight Matches in Color Description: Highlights matching strings in color, making them easier to spot in the output. Code: grep --color=auto "search_string" [file] Code: grep --color=auto "error" /var/log/syslog 16. Exclude Specific Files from Search Description: Excludes certain files from being searched, useful when searching in directories. Code: grep --exclude=[file_pattern] "search_string" [directory] Code: grep --exclude=*.log "error" /var/log/* 17. Exclude Directories from Recursive Search Description: Excludes specific directories from being searched when performing a recursive search. Code: grep --exclude-dir=[dir_pattern] -r "search_string" [directory] Code: grep --exclude-dir=archive -r "error" /var/log 18. Limit the Number of Matches Description: Stops searching after a specified number of matches are found. Code: grep -m [num] "search_string" [file] Code: grep -m 5 "error" /var/log/syslog 19. Display Non-Matching Files (In Binary Mode) Description: Displays files that do not contain matches, useful for binary files. Code: grep -L -I "search_string" [files] Code: grep -L -I "magic" /bin/* 20. Perform a Binary Search Description: Searches for matches in binary files and displays them. Code: grep -a "search_string" [binary_file] Code: grep -a "magic" /bin/somebinaryfile Conclusion The **grep** command is a powerful tool for searching and filtering text within files and streams, making it an essential utility in the Unix/Linux command-line toolkit. By mastering these commands, you can efficiently search, analyze, and process large amounts of text data. Happy Searching! |