How To Grep Without the File Name
Global regular expression print or just grep is one of the most versatile and common commands in Unix and Linux systems. The command looks for single or multiple input files and matching pattern lines. As a result, you get a standard output with the matching lines.
If you don’t specify a file, grep uses the standard input for the readouts and it’s most likely going to be another command’s output. On the whole, grep features straightforward syntax whether you use it with or without the file name.
This article provides you with a quick overview of the grep syntax and some basic commands you can use with or without the file name.
Before You Start
All grep commands follow the same syntax and each parameter has a specific function. Here is the sample line:
grep [OPTIONS] PATTERN [FILE…]
You can use a number of OPTIONS in grep to control the command output and the number starts at zero. PATTERN indicates the search pattern you want to apply. As for the FILE, this is where the file name or names go, but the parameter can be set to zero.
String Search in Command Output
As indicated, you don’t need to use specific input files. Another command’s output can be used in grep to get lines that exactly match a pattern. This can be used to determine which processes are active on the system. This is sample command syntax:
$ ps -ef | grep www-data
The output you get should look something like this:
www-data 18247 12675 4 16:00 ? 00:00:00 php-fpm: pool www
root 18272 17714 0 16:00 pts/0 00:00:00 grep –color=auto –exclude-dir=.bzr –exclude-dir=CVS –exclude-dir=.git –exclude-dir=.hg –exclude-dir=.svn www-data
www-data 31147 12770 0 Oct22 ? 00:05:51 nginx: worker process
www-data 31148 12770 0 Oct22 ? 00:00:00 nginx: cache manager process
There is a way to exclude the command line that features grep processes. For this, you need to use the $ ps -ef | grep www-data | grep -v grep command.
Grep Word Search
You can use grep commands to search for specific words in files on your system. In fact, there is no need to type an entire word. You can just use gnu, for example, and the command outputs all the words that have these three letters in them. The command syntax is:
$ grep gnu /usr/share/words
After executing the command, your output should look something like this:
cygnus
gnu
interregnum
lgnu9d
lignum
magnum
magnuson
sphagnum
wingnut
On the other hand, you can search just for that specific word or string of letters and exclude everything else. For this, you need to add -w or –word – regexp to the syntax. In this case, the example command looks like this – $ grep -w gnu /usr/share/words.
Note: For grammar purposes, some of the commands in this article have a full stop at the end. You don’t need that punctuation mark for grep. Exclude it when you copy/paste the command.
Is Grep Case Sensitive?
All grep commands are case sensitive by default. This means using the lowercase and uppercase characters makes a difference in the command itself. However, you can add – i (— ignore – case) to the command line and allow the system to search for both upper and lower case words.
For example, your input command may look like this $ grep -i Zebra /usr/share/words. It allows the output to match any combination of upper and lower case letters when it searches for zebra.
Line Numbers
Use the –line-number option or just -n to determine the number of lines with a string which matches a particular search pattern. As a result, you get a standard output with a line number in front of it.
The exact command may look like this: $ grep -n 10000 /etc/services. Upon execution, the output provides all the matches it finds on 10000 lines. Check out the sample below:
10423:ndmp 10000/tcp
10424:ndmp 10000/udp
Files in Folders
You can put an asterisk behind a grep command instead of a file name. Using the gnu criteria again the command looks like this $ grep gnu * and the output lists the files that contain gnu in. It’s important to note that this kind of command returns a line.
Note: With grep, a line refers to a sequence of characters that run up to a specified break. Unless you refine the search, the output may contain entire paragraphs of information.
Take the Next Step with Grep
This guide just scratches the surface of what you can do with grep. It might take a while to get all the odds and ends, but the syntax follows a fairly simple principle. And with some practice, you’ll be able to refine your searches to perfection, with or without the file name.