Using grep to filter results
The command line utility “grep” is one of the most powerful and useful tools in Linux. Its most common use is to filter results from everyday commands. For instance, if you want to see all the hostnames your system has mapped out in /etc/hosts you can simply run:
$ cat /etc/hosts
But if you know you’re just looking for a specific hostname, you can pipe the output through grep:
$ cat /etc/hosts | grep head
10.3.1.254 head-ib head-ib.cluster
10.2.1.254 head-mgmt head-mgmt.cluster
10.2.1.253 head-ipmi head-ipmi.cluster
10.1.1.254 head head.cluster
This limited the search results to lines that contained the word “head,” which represents this cluster’s head node.
Another very useful grep technique is to find every instance of a word or pattern within every file in a directory or even subdirectories:
$ grep -R “words to search for” /path/to/search/in/
This will scan every file within the path you specify and find any instance of the word or words you listed.
These two examples are just the beginning of what you can do with grep. To get the full manual for the grep command, simply type:
$ man grep
Request a Consultation from our team of HPC and AI Experts
Would you like to speak to one of our HPC or AI experts? We are here to help you. Submit your details, and we'll be in touch shortly.