Using grep
When I first started working in systems integration, I was primarily a PC support person. I spent a lot of time installing and supporting Windows applications in various PC LAN configurations, running various versions (and vendors) of TCP/IP transports. Since then, I have successfully ditched DOS and moved on. Now, after working on various versions of Unix for a few years, I porting some of our networking and data manipulation libraries to other platforms and environments, such as the AS/400 minicomputer and the Macintosh. This ongoing experience has given me a chance to appreciate just how powerful the tools we take for granted with Linux really are.
Searching for a word (or any other value) in a group of files is a very common task. Whether it's searching for a function in a group of source code modules, trying to find a parameter in a set of configuration files, or simply looking for a misplaced e-mail message, text searching and matching operations are common in all environments.
Unfortunately, this common task doesn't have an easy solution on all platforms. On most, the best solution available is to use the search function in an editor. But when it comes to Linux (and other Unix descendants), you have many solutions. One of them is grep.
grep is an acronym for “global regular expression print,” a reference to the command in the old ed line editor that prints all of the lines in a file containing a specified sequence of characters. grep does exactly that: it prints out lines in a file that contain a match for a regular expression. We'll gradually delve into what a regular expression is as we go on.
First, let's look at a quick example. We will search for a word in the Configure script provided with Linux for setting up the Linux kernel source, which is usually installed in the /usr/src/linux directory. Change to that directory and type (the $ character is the prompt, don't type it):
$ grep glob Configure
You should see:
# Disable filename globbing once and for all.
glob is in bold to illustrate what grep matched. grep does not actually print matches in bold.
grep looked for the sequence of characters glob and printed the line of the Configure file with that sequence. It did not look for the word glob. It looked for g followed by l followed by o followed by b. This points out one important aspect of regular expressions: they match sequences of characters, not words.
Before we dig any deeper into the specifics of pattern matching, let's look at grep's “user interface” with a few examples. Try the following two commands:
$ grep glob < Configure $ cat Configure | grep glob
both of these two commands should print
# Disable filename globbing once and for all.
which probably looks familiar.
In all of these commands, we have specified the regular expression as the first argument to grep. With the exception of any command line switches, grep always expects the regular expression as the first argument.
However, we presented grep with three different situations and received the same response. In the first exercise, we provided grep with the name of a file, and it opened that file and searched it. grep can also take a list of filenames to search.
In the other two exercises we illustrated a feature that grep shares with many other utilities. If no files are specified on the command line, grep reads standard input. To further illustrate standard input let's try one more example:
$ grep foo
When you run that, grep appears to “hang” waiting for something. It is. It's waiting for input. Type:
tttt
and press return. Nothing happens. Now type:
foobar
and press enter. This time, grep sees the string foo in foobar and echos the line foobar back at you, which is why foobar appears twice. Now type ctrl-d, the “end-of-file” character, to tell grep that it has reached the end of the file, whereupon it exits.
You just gave grep an input file that consisted of tttt, a newline character, foobar, a newline character, and the end-of-file character.
Piping input into grep from standard input also has another frequent use: filtering the output of other commands. Sometimes cutting out the unnecessary lines with grep is more convenient than reading output page by page with more or less:
$ ps ax | grep cron
efficiently gives you the process information for crond.
Trending Topics
| Analyze Song Lyrics with a Shell Script, Part II | Jan 09, 2018 |
| Linux Filesystem Events with inotify | Jan 08, 2018 |
| What I See for LJ 2.0: in a Word, Community | Jan 08, 2018 |
| Emacs for Science | Jan 07, 2018 |
| Let's talk advertising | Jan 06, 2018 |
| How-to Take the Plunge and Replace the "I'm not a programmer" Mantra | Jan 06, 2018 |
- Linux Filesystem Events with inotify
- Analyze Song Lyrics with a Shell Script, Part II
- Happy New Year- Welcome to Linux Journal 2.0!
- Emacs for Science
- Understanding Firewalld in Multi-Zone Configurations
- The Weather Outside Is Frightful (Or Is It?)
- IGEL Universal Desktop Converter
- Linux Journal February 2017
- Simple Server Hardening
- From vs. to + for Microsoft and Linux
Geek Guides
Pick up any e-commerce web or mobile app today, and you’ll be holding a mashup of interconnected applications and services from a variety of different providers. For instance, when you connect to Amazon’s e-commerce app, cookies, tags and pixels that are monitored by solutions like Exact Target, BazaarVoice, Bing, Shopzilla, Liveramp and Google Tag Manager track every action you take. You’re presented with special offers and coupons based on your viewing and buying patterns. If you find something you want for your birthday, a third party manages your wish list, which you can share through multiple social- media outlets or email to a friend. When you select something to buy, you find yourself presented with similar items as kind suggestions. And when you finally check out, you’re offered the ability to pay with promo codes, gifts cards, PayPal or a variety of credit cards.
Get the Guide


Comments
Some confuusion while searching using grep
I hav a dir and on that file present like this
-rw-r--r-- 1 vs users 1390254 Jun 2 01:00 abcdefg.1006010100.dat
-rw-r--r-- 1 vs users 1388800 Jun 3 01:00 abcdefg.1006020100.dat
-rw-r--r-- 1 vs users 1388555 Jun 4 01:00 abcdefg.1006030100.dat
-rw-r--r-- 1 vs users 1392184 Jun 5 01:00 abcdefg.1006040100.dat
-rw-r--r-- 1 vs users 1391747 Jun 6 01:00 abcdefg.1006050100.dat
-rw-r--r-- 1 vs users 1392099 Jun 7 01:00 abcdefg.1006060100.dat
-rw-r--r-- 1 vs users 1389362 Jun 8 01:00 abcdefg.1006070100.dat
-rw-r--r-- 1 vs users 1392676 Jun 9 01:00 abcdefg.1006080100.dat
-rw-r--r-- 1 vs users 1436696 Jun 10 01:00 abcdefg.1006090100.dat
-rw-r--r-- 1 vs users 1060539 Jun 10 18:39 abcdefg.1006100100.dat
please check output while i am using grep command in below manner ..
vs@vodalksmvs2 /var/opt/nokia/smvs/tmp > ls -ltr | grep abc*
vs@vodalksmvs2 /var/opt/nokia/smvs/tmp > ls -ltr | grep "abc*"| tail
-rw-r--r-- 1 vs users 1390254 Jun 2 01:00 abcdefg.1006010100.dat
-rw-r--r-- 1 vs users 1388800 Jun 3 01:00 abcdefg.1006020100.dat
-rw-r--r-- 1 vs users 1388555 Jun 4 01:00 abcdefg.1006030100.dat
-rw-r--r-- 1 vs users 1392184 Jun 5 01:00 abcdefg.1006040100.dat
-rw-r--r-- 1 vs users 1391747 Jun 6 01:00 abcdefg.1006050100.dat
-rw-r--r-- 1 vs users 1392099 Jun 7 01:00 abcdefg.1006060100.dat
-rw-r--r-- 1 vs users 1389362 Jun 8 01:00 abcdefg.1006070100.dat
-rw-r--r-- 1 vs users 1392676 Jun 9 01:00 abcdefg.1006080100.dat
-rw-r--r-- 1 vs users 1436696 Jun 10 01:00 abcdefg.1006090100.dat
-rw-r--r-- 1 vs users 1059029 Jun 10 18:37 abcdefg.1006100100.dat
vs@vodalksmvs2 /var/opt/nokia/smvs/tmp > ls -ltr | grep abc| tail
-rw-r--r-- 1 vs users 1390254 Jun 2 01:00 abcdefg.1006010100.dat
-rw-r--r-- 1 vs users 1388800 Jun 3 01:00 abcdefg.1006020100.dat
-rw-r--r-- 1 vs users 1388555 Jun 4 01:00 abcdefg.1006030100.dat
-rw-r--r-- 1 vs users 1392184 Jun 5 01:00 abcdefg.1006040100.dat
-rw-r--r-- 1 vs users 1391747 Jun 6 01:00 abcdefg.1006050100.dat
-rw-r--r-- 1 vs users 1392099 Jun 7 01:00 abcdefg.1006060100.dat
-rw-r--r-- 1 vs users 1389362 Jun 8 01:00 abcdefg.1006070100.dat
-rw-r--r-- 1 vs users 1392676 Jun 9 01:00 abcdefg.1006080100.dat
-rw-r--r-- 1 vs users 1436696 Jun 10 01:00 abcdefg.1006090100.dat
-rw-r--r-- 1 vs users 1060539 Jun 10 18:39 abcdefg.1006100100.dat
vs@vodalksmvs2 /var/opt/nokia/smvs/tmp > ls -ltr | grep abc.*| tail -2
-rw-r--r-- 1 vs users 1436696 Jun 10 01:00 abcdefg.1006090100.dat
-rw-r--r-- 1 vs users 1060539 Jun 10 18:39 abcdefg.1006100100.dat
vs@vodalksmvs2 /var/opt/nokia/smvs/tmp > ls -ltr | grep *abc| tail -2
vs@vodalksmvs2 /var/opt/nokia/smvs/tmp > ls -ltr | grep *.abc| tail -2
vs@vodalksmvs2 /var/opt/nokia/smvs/tmp >
I want to know in what manner grep runs and giving output for above commands.
grep (-A|-B|-C)
I use linux as my development environment. Grep with -A, -B and -C option provides me with context for what I'm searching. These options print out lines either above or below the target line.
For example I search for a function call and get grep to display the lines above and/or below the said line.
grep -p
I come from the AIX world. We have many scripts that use grep -p to get the paragraph containing a search string. I cannot find anything comparable to grep -p in Red Hat Linux. Any ideas?
grep -p
Never mind, I found this code. (Shoulda done more googling first! :-) )
#!/bin/sh
# usage: pargrep
inFile="$1"
searchString="$2"
awk '
BEGIN {
FS="\n"
RS=""
}
/'"$searchString"'/ { print }
' ${inFile}
non-standard args - could be made less confusing
That's good as far as it goes, but you should reverse $1 and $2. NORMAL grep is:
grep
That pargrep is
grep
At least you should minimize the differences. Also, if you go with the "standard" way, a very small change to that script could be made to search across multiple files, just like grep would.
[sigh] stupid html. Ok, that
[sigh] stupid html.
Ok, that should have read:
...
grep pattern filename
that pargrep is:
grep filename pattern
...
Question
Dear Sir,
How can i search in my lunix server for a word in the sources code for any file that is located in the vhost. for example:
I need to search for the word "iframe" in all the "vhosts" folder on my server. this word is in the "Sources code for all my files for my websites"
Can u help me in this please ?
i will too much greatfull for you
regards
samer
The fastest method is...
The following, replacing "*.php" by your source files' extension:
$ find . -name "*.php" -print0 | xargs -0 grep iframeSearch
There are a number of ways of doing that, try:
$ find /path/to/vhosts -type f -exec grep --with-filename iframe {} \;or try:
Mitch Frazier is an Associate Editor for Linux Journal.