grep Commands Working in Linux
grep is an essential command utility used in Linux and Unix environments to search text and strings in a given file. In other words, we can say that grep command search provides the lines containing a match strings or words in the given file. It is one of the most useful commands on Linux systems for Developers and testers for debugging the system logs. In this blog posts, Let us dicuss what are different ways we can used grep on a Linux system.
- grep stands for global regular expression and print
- When grep finds a match, it prints the line with the result
- The grep command is handy tool for searching known text or string through large log files
- egrep, and fgrep are other similar tools
- grep requires access to a terminal/command line
- grep user requires read/write permissions to access the desired files and directories
- grep does not work in the Windows environment
grep Command Syntax
- -c : This prints a count of the lines that match the pattern
- -h : Display the matched lines, but do not display the filenames
- -i : Ignores upper/lower case for matching
- -l : Displays list of a filenames only
- -n : Display the matched lines and their line numbers
- -v : This prints out all the lines that do not matches the pattern
- -e exp : Specifies expression with this option. Can use multiple times
- -f file : Takes patterns from file, one per line
- -E : Treats pattern as an extended regular expression
- -w : Match whole word
- -o : Print only the matched parts of a matching line, with each such part on a separate output line
grep Command Examples
- grep with ingore case: grep with option -i search for the given text and igore the upper/lower case. For example if you want to search error in a file, you can put error or Error in search text will ingore and provide all related matching lines. Following picture show the same.
- grep with count: grep with option -c search and provide the number of count for a text or string appeared in the file. This is useful when we want to know how many time one particular error happen in the log file. count option is case sensitive, be sure about the actual search string. Following picture show the example where we can see error (small letter) appeared two time in the log file an Error (with captil ‘E’) appeared 22 time in the event-log file.
- grep display with Line numbers: grep with option -n print the line number where the search text is present. This will help user to go the line when user open the file in vi editer to know nearby logs. Following picture show the example for the line numbers.
- Linux Basic Commands
- tar.gz – Linux tar command examples
- 4G LTE Important QXDM Log Packets
- Iperf : A Tool for Network Testing
- tcpdump for Linux System: A Tool for IP Packet Analysis
- What are the most common Test Cases for 5G Device Verification?