Grep And Its Magic
Grep in Linux is a very useful command-line utility used for searching data sets of specific files for lines that match regular expressions of plain texts.
Grep stands for Global Regular Expression Print.
In our daily routine of software development or any other profession that involves continuous use of Linux have to go through large swaths of text either they may be any logs on the server or user activity. Grep may filter out a specific line of text you are searching for, which decreases your working complexity and time. If you are familiar with regular expression it might be more effective in filtering.
Listing here a few examples to see how Grep command helps you in your tasks .
How to search for a text from a file ?
Answer : grep "search text" filename
eg: grep docker /etc/group
How to do a case-insensitive search of a text?
Answer : Use grep command with -i option . -i is used to find for texts irrespective of case .
eg : grep -i 'shraddha' /etc/group
How to search for the exact word in a file ?
Answer : Use grep command with -w option .
eg: grep -w 'docker' /etc/group
How to search for a line or word along with line numbers ?
Answer : Use -n to get the solution with line number .
Eg: grep -n 'docker' /etc/group
How to list files and directories using grep command ?
Answer : use ls command with grep . ls command is used if the no. of files and directories are less but if they are more and you want to find a specific text among huge number of files then better use ls and grep together .
eg: ls -l /etc | grep sudo
How to display the count of number of lines that matches the given string/pattern?
Answer : use -c option to get the number of lines that matches the given string/pattern .
Eg: grep -c "shraddha" /etc/passwd
How to search process status ?
Answer : ps cmd is used to get the process status pipe this cmd with grep and you will get the solution . Most commonly used command .
eg: ps aux | grep -w tomcat
How to search socker statistics using grep ?
Answer : use ss command it gives you information of socket activity and port listening . it prints listening sockets and tcp sockets .
How to search comands from history commands output ?
Answer : history | grep -i systemctl
How to search multiple patterns at the same time ?
Answer : use -e before every pattern .
Eg: grep -e sudo -e adm /etc/group
How to select matches at the beginning and ending ?
Answer : grep -i "^t" filename.txt $ grep -i "t$" filename.txt
In the above example, we use special characters ^ and $ where ^ denotes started with and $ denotes ended with. The first command will print all the lines that start with ‘t’ and the second command will print all the line that ends with ‘t’.
How to filter the filenames that contain a particular text or pattern ?
Answer : use -l option it displays only the file names that has a particular text/pattern that you are searching .
eg : grep -l "unix" /home/ubuntu
Subscribe to my newsletter
Read articles from Shraddha Bandhakavi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by