Grep
Grep
Grep
Linux grep command FAQ: Can you share some Linux/Unix grep command examples? Sure. Think of the Linux/Unix grep command as a "search" command for Unix and Linux system. (Many people wish it was named "search"). It's used to search for text strings and regular expressions within one or more files. I think it's easiest to learn something like this by showing a collection of examples, so let's take a look at some grep command examples.
grep 'joe' *
The * wildcard matches all files in the current directory. (More accurately, it matches all files in the current directory that don't begin with a decimal character, and it will also match any subdirectories in the current directory, but that usually isn't much of a problem.) As another example, you can also use grep to search all files in the current directory that end in the file extension ".txt", as shown here:
root nobody nobody nobody nobody nobody nobody nobody nobody nobody root grep http
17937 21538 24481 26089 27842 27843 27911 28280 30404 31895 31939
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
14760
6880 ?
Ss S S S S S S S S S R+
Apr01 Apr03 Apr03 Apr03 Apr03 Apr03 Apr03 Apr03 Apr03 Apr03 Apr03
0:39 0:01 0:00 0:01 0:00 0:00 0:01 0:00 0:00 0:00 0:00
/usr/local/apache/bin/httpd -k start 24372 17108 ? 14760 6396 ? /usr/local/apache/bin/httpd -k start /usr/local/apache/bin/httpd -k start 24144 16876 ? 24896 17636 ? 24192 16936 ? 23888 16648 ? 24664 17256 ? 24360 17112 ? 14760 1848 6296 ? 548 pts/0 /usr/local/apache/bin/httpd -k start /usr/local/apache/bin/httpd -k start /usr/local/apache/bin/httpd -k start /usr/local/apache/bin/httpd -k start /usr/local/apache/bin/httpd -k start /usr/local/apache/bin/httpd -k start /usr/local/apache/bin/httpd -k start
(I deleted about half of the "httpd -k start" lines from that output manually to save a little space.) Similarly, here's how you can find all the Java processes running on your system using the ps and grep commands in a Unix pipeline:
drwxr-x--- 14 akamtikar nse 4096 Oct 21 16:04 Batch_Data drwxr-x--- 17 akamtikar nse 4096 Nov 8 17:34 batches drwxrwxrwx 18 akamtikar nse 4096 Nov 3 19:05 Binary_Data drwxrwxrwx 2 akamtikar nse 4096 Oct 22 17:39 Build drwxr-x--- 7 akamtikar nse 4096 Nov 8 17:47 config drwxrwxrwx 6 akamtikar nse 4096 Oct 21 16:04 data drwxrwxrwx 4 akamtikar nse 4096 Nov 1 12:29 etc drwxrwxrwx 4 akamtikar nse 4096 Nov 16 17:48 FOTS -rwxrwxrwx 1 akamtikar nse 516 Oct 21 13:11 FOTS_CHANGE_TRADING_DATE_IN drwxr-x--- 2 akamtikar nse 4096 Oct 21 16:03 Login_Topicwise drwxr-x--- 11 akamtikar nse 4096 Nov 12 19:16 logs drwxr-x--- 7 akamtikar nse 4096 Oct 21 16:04 Publisher_Utility drwxr-x--- 7 akamtikar nse 4096 Oct 21 16:04 Regression_Utility_Data -rwxr-x--- 1 akamtikar nse 464 Oct 21 16:03 removeagentlogs.sh -rwxrwxrwx 1 akamtikar nse 369 Oct 21 18:41 removelogs.sh -rwxrwxrwx 1 akamtikar nse 91 Oct 11 15:26 removeshm.sh
drwxr-x--- 10 akamtikar nse 4096 Oct 21 18:43 safestores drwxr-x--- 4 akamtikar nse 4096 Nov 8 17:40 scripts -rwxr-x--- 1 akamtikar nse 516 Oct 21 16:03 Shutdown_BE -rwxr-x--- 1 akamtikar nse 516 Oct 21 16:03 STARTUP_IN_BE
drwxr-x--- 2 akamtikar nse 4096 Oct 21 16:09 UTIL_config drwxr-x--- 3 akamtikar nse 4096 Oct 21 16:03 VULCAN-DEV drwxr-x--- 4 akamtikar nse 4096 Oct 21 16:04 vulcanproc drwxr-x--- 8 akamtikar nse 4096 Nov 4 21:05 Working_config
-rwxr-x--- 1 akamtikar nse 516 Oct 21 16:03 Shutdown_BE -rwxr-x--- 1 akamtikar nse 516 Oct 21 16:03 STARTUP_IN_BE
drwxr-x--- 14 akamtikar nse 4096 Oct 21 16:04 Batch_Data drwxr-x--- 17 akamtikar nse 4096 Nov 8 17:34 batches drwxrwxrwx 18 akamtikar nse 4096 Nov 3 19:05 Binary_Data drwxrwxrwx 2 akamtikar nse 4096 Oct 22 17:39 Build drwxr-x--- 7 akamtikar nse 4096 Nov 8 17:47 config drwxrwxrwx 6 akamtikar nse 4096 Oct 21 16:04 data drwxrwxrwx 4 akamtikar nse 4096 Nov 1 12:29 etc drwxrwxrwx 4 akamtikar nse 4096 Nov 16 17:48 FOTS
drwxr-x--- 2 akamtikar nse 4096 Oct 21 16:03 Login_Topicwise drwxr-x--- 11 akamtikar nse 4096 Nov 12 19:16 logs drwxr-x--- 7 akamtikar nse 4096 Oct 21 16:04 Publisher_Utility drwxr-x--- 7 akamtikar nse 4096 Oct 21 16:04 Regression_Utility_Data drwxr-x--- 10 akamtikar nse 4096 Oct 21 18:43 safestores drwxr-x--- 4 akamtikar nse 4096 Nov 8 17:40 scripts drwxr-x--- 2 akamtikar nse 4096 Oct 21 16:09 UTIL_config drwxr-x--- 3 akamtikar nse 4096 Oct 21 16:03 VULCAN-DEV drwxr-x--- 4 akamtikar nse 4096 Oct 21 16:04 vulcanproc drwxr-x--- 8 akamtikar nse 4096 Nov 4 21:05 Working_config
6) Using the Linux grep command to search for multiple patterns at one time (egrep)
You can use a different version of the grep command to search for multiple patterns at one time. To do this, just use the "egrep" command instead of grep, like this:
-v display those lines that do NOT match -n precede each matching line with the line number -c print only the total count of matched lines
grep '[FG]oo' * [akamtikar@draco9 publisher]$ cat names good food mood sood tood hood cood bood zood tood wood nood pood [akamtikar@draco9 publisher]$ grep names:food names:mood [akamtikar@draco9 publisher]$ grep names:food names:mood names:tood names:tood names:pood [akamtikar@draco9 publisher]$ grep -a '[gmpsf]ood' * '[mptf]ood' * '[mf]ood' *
36::
26::printGeneralFields::src/fots/structures/orderentry/generalfield pt2pubsrvc_0_1.log:Nov 12, 2010; 19:27:27:: DEBUG:: s.c[ 100]:: goodTillDate = 0 36:: 36::
grep '[0-9][0-9][0-9]' *
This next grep command searches for all occurrences of the text string fred within the "/etc/passwd" file, but also requires that the "f" in the name "fred" be in the first column of each record (that's what the caret character tells grep). Using this more-advanced search, a user named "alfred" would not be matched, because the letter "a" will be in the first column.
grep '^fred' /etc/passwd [akamtikar@draco9 publisher]$ grep '^cood' * names:cood [akamtikar@draco9 publisher]$ grep '^tood' * names:tood names:tood
Regular expressions can get much, much more complicated (and powerful) than this, so I'll just leave it here for now.
123nischal 231nischal 331nischal 431nischal 531nischal 631nischal 731nischal 132nischal 123nischal 123nischal 431nischal 555nischal
net2outmsg.log:0 net2pubsrvc_0_1.log:0 net2pubsrvc_0.lfh:0 net2pubsrvc.metrics:0 passivemkt1pubsrvc_0_1.log:30 passivemkt1pubsrvc_0.lfh:0 passivemkt1pubsrvc.metrics:0 postprocpubhbsrvc_0_1.log:0 postprocpubhbsrvc_0.lfh:0 postprocpubhbsrvc.metrics:0 pt2outmsg.log:0 pt2pubsrvc_0_1.log:0 pt2pubsrvc_0.lfh:0 pt2pubsrvc.metrics:0 [akamtikar@draco9 publisher]$ grep -aic 'transcode = 9002' * cosoutmsg.log:0 cospubsrvc_0_1.log:0 cospubsrvc_0.lfh:0 cospubsrvc.metrics:0 dsnormaloutmsg.log:0 dsnormalpubsrvc_0_1.log:0 dsnormalpubsrvc_0.lfh:0 dsnormalpubsrvc.metrics:0 dspriorityoutmsg.log:0 dsprioritypubsrvc_0_1.log:0 dsprioritypubsrvc_0.lfh:0
dsprioritypubsrvc.metrics:0 net1outmsg.log:0 net1pubsrvc_0_1.log:25 net1pubsrvc_0.lfh:0 net1pubsrvc.metrics:0 net2outmsg.log:0 net2pubsrvc_0_1.log:0 net2pubsrvc_0.lfh:0 net2pubsrvc.metrics:0 passivemkt1pubsrvc_0_1.log:30 passivemkt1pubsrvc_0.lfh:0 passivemkt1pubsrvc.metrics:0 postprocpubhbsrvc_0_1.log:0 postprocpubhbsrvc_0.lfh:0 postprocpubhbsrvc.metrics:0 pt2outmsg.log:0 pt2pubsrvc_0_1.log:0 pt2pubsrvc_0.lfh:0 pt2pubsrvc.metrics:0 [akamtikar@draco9 publisher]$ egrep -ic 'transcode = 9002|transcode = 9004| transcode = 2070|transcode = 2106' * cosoutmsg.log:0 cospubsrvc_0_1.log:0 cospubsrvc_0.lfh:0 cospubsrvc.metrics:0 dsnormaloutmsg.log:0 dsnormalpubsrvc_0_1.log:0
dsnormalpubsrvc_0.lfh:0 dsnormalpubsrvc.metrics:0 dspriorityoutmsg.log:0 dsprioritypubsrvc_0_1.log:0 dsprioritypubsrvc_0.lfh:0 dsprioritypubsrvc.metrics:0 net1outmsg.log:0 net1pubsrvc_0_1.log:55 net1pubsrvc_0.lfh:0 net1pubsrvc.metrics:0 net2outmsg.log:0 net2pubsrvc_0_1.log:0 net2pubsrvc_0.lfh:0 net2pubsrvc.metrics:0 passivemkt1pubsrvc_0_1.log:117 passivemkt1pubsrvc_0.lfh:0 passivemkt1pubsrvc.metrics:0 postprocpubhbsrvc_0_1.log:0 postprocpubhbsrvc_0.lfh:0 postprocpubhbsrvc.metrics:0 pt2outmsg.log:0 pt2pubsrvc_0_1.log:0 pt2pubsrvc_0.lfh:0 pt2pubsrvc.metrics:0
If you're looking through a lot of files for a pattern, and you just want to find the names of the files that contain your pattern (or "patterns", as shown with egrep) -- but don't want to see each individual grep pattern match -just add the "-l" (lowercase letter L) to your grep command, like this:
[akamtikar@draco9 publisher]$ egrep -il 'transcode = 9002|transcode = 9004| transcode = 2070|transcode = 2106' * net1pubsrvc_0_1.log passivemkt1pubsrvc_0_1.log
[akamtikar@draco9 publisher]$ grep 'let' ngrepfilename Linux grep command FAQ: Can you share let's some Linux/Unix grep command examples? Think of the Linux/Unix grep let's command as a "search" command for Unix and Linux system Many people wish it was named "search" let's
It's used to search for text strings let's and regular expressions within one or more files I think it's easiest to learn let's something like this by showing a collection of examples so let's take a look at some grep command examples.
[akamtikar@draco9 publisher]$ grep -n 'let' ngrepfilename 1:Linux grep command FAQ: Can you share let's some Linux/Unix grep command examples? 2:Think of the Linux/Unix grep let's command as a "search" command for Unix and Linux system 3:Many people wish it was named "search" let's 4:It's used to search for text strings let's and regular expressions within one or more files 5:I think it's easiest to learn let's something like this by showing a collection of examples 6:so let's take a look at some grep command examples.
10) grep before/after - Showing lines before or after your grep pattern match
After a recent comment, I just learned that you can display lines before or after your grep pattern match, which is also very cool. To display five lines before the phrase "the living" in my sample document, use the -B argument, like this:
The world will little note, nor long remember, what we say here, but can never forget what they did here.
It is for us, the living, rather to be dedicated here to the unfinished work which they have, thus far, so nobly carried on. It is rather for us to be here dedicated to the great task remaining before us Of course you can use any number after the -A and -B options, I'm just using the number five here as an example. [akamtikar@draco9 publisher]$ grep -B 5 "look" ngrep Linux grep command FAQ: Can you share let's some Linux/Unix grep command examples? Think of the Linux/Unix grep let's command as a "search" command for Unix and Linux system Many people wish it was named "search" let's It's used to search for text strings let's and regular expressions within one or more files I think it's easiest to learn let's something like this by showing a collection of examples so let's take a look at some grep command examples. [akamtikar@draco9 publisher]$ grep -A 5 "FAQ" ngrep Linux grep command FAQ: Can you share let's some Linux/Unix grep command examples? Think of the Linux/Unix grep let's command as a "search" command for Unix and Linux system Many people wish it was named "search" let's
It's used to search for text strings let's and regular expressions within one or more files I think it's easiest to learn let's something like this by showing a collection of examples so let's take a look at some grep command examples.
"." means "look in the current directory" "-type f" means "look in files only" "-exec grep -il foo" means "search for the string 'foo' in a case-insensitive manner, and return the matching line and filename when a match is found "{} \;" is some bizarre find syntax that you need to add to the end of your find command whenever you add the -exec option. (Sorry for my opinion there ... but you have to agree, that syntax is a little unusual.)
Note that on Mac OS X systems you may be able to use the mdfind command instead of this find/grep combination command. The mdfind command is a command-line equivalent of the Spotlight search functionality.
The locate command is more related to the find command, but I thought I would note that it is good at finding files in the entire filesystem when you know the filename, or part of the filename. And as I mentioned in the previous section Mac OS X systems have the mdfind command. As a practical matter I use plain old grep 99% of the time.
"." means "look in the current directory" "-type f" means "look in files only" "-exec grep -il foo" means "search for the string 'foo' in a case-insensitive manner, and return the matching line and filename when a match is found "{} \;" is some bizarre find syntax that you need to add to the end of your find command whenever you add the -exec option. (Sorry for my opinion there ... but you have to agree, that syntax is a little unusual.)
Note that on Mac OS X systems you may be able to use the mdfind command instead of this find/grep combination command. The mdfind command is a command-line equivalent of the Spotlight search functionality.
The locate command is more related to the find command, but I thought I would note that it is good at finding files in the entire filesystem when you know the filename, or part of the filename. And as I mentioned in the previous section Mac OS X systems have the mdfind command. As a practical matter I use plain old grep 99% of the time.
[akamtikar@draco9 publisher]$ find /safestore3/Nischal/ -type f -exec grep -il "NavinNischal" {} \; /safestore3/Nischal/logs/postproc/active/publisher/ngrep