For this purpose the special metacharacter \ is used to escape metacharacters. Escaped metacharacters are interpreted literally, not as a component of an expression. Therefore \[ would match any sequence with a [ in it:
$ grep '[' CREDITS
produces an error message:
grep: Unmatched [ or [^
$ grep '\[' CREDITS
produces two lines:
E: email@example.com [My uucp-fed Linux box at home] D: The XFree86[tm] Project
If you need to search for a \ character, escape it just like any other metacharacter: \\
As you can see, with just its support of regular expression syntax, grep provides us with some very powerful capabilities. Its command line options add even more power.
Sometimes you are looking for a string, but don't know whether it is upper, lower, or mixed case. For this situation grep offers the -i switch. With this option, case is completely ignored:
$ grep -i lINuS CREDITS Linus N: Linus Torvalds E: Linus.Torvalds@Helsinki.FI D: Personal information about Linus
The -v option causes grep to print all lines that do not contain the specified regular expression:
$ grep -v '^#" /etc/syslog.conf | grep -v '^$'
prints all the lines from /etc/syslog.com that are neither commented (starting with #) nor empty (^$). This prints six lines on my system, although my syslog.conf file really has 21 lines.
If you need to know how many lines match, pass grep the -c option. This will output the number of matching lines (not the number of matches; two matches in one line count as one) without printing the lines that match:
$ grep -c Linux CREDITS 33
If you are searching for filenames that contain a given string, instead of the actual lines that contain it, use grep's -l switch:
$ grep -l Linux * CREDITS README README.modules
grep also notifies us, for each subdirectory, that it can't search through a directory. This is normal and will happen whenever you use a wildcard that happens to include directory names as well as file names.
The opposite of -l is -L. This option will cause grep to return the names of files that do not contain the specified pattern.
If you are searching for a word and want to suppress matches that are partial words use the -w option. Without the -w option,
$ grep -c a README
tells us that it matched 146 lines, but
$ grep -wc a README
returns only 35 since we matched only the word a, not every line with the character a.
Two more useful options:
$ grep -b Linus CREDITS 301: Linus 17446:N: Linus Torvalds 17464:E: Linus.Torvalds@Helsinki.FI 20561:D: Personal information about Linus $ grep -n Linus CREDITS 7: Linus 793:N: Linus Torvalds 794:E: Linus.Torvalds@Helsinki.FI 924:D: Personal information about Linus
The -b option causes grep to print the byte offset (how many bytes the match is from the beginning of the file) of each match before the corresponding line of output. The -n switch gives the line number.
GNU also provides egrep (enhanced grep). The regular expression syntax supported by GNU egrep adds a few other metacharacters:
? Like *, except that it matches zero or one instances instead of zero or more.
+ the preceding character is matched one or more times.
| separates regular expressions by ORing them together.
$ egrep -i 'linux|linus' CREDITS
outputs any line that contains linus or linux.
To allow for legibility, parentheses “(” and “)” can be used in conjunction with “|” to separate and group expressions.
This covers many of the features provided by grep. If you look at the manual page, which I strongly recommend, you will see that I did leave out some command-line options, such as different ways to format grep's output and a method for searching for strings without employing regular expressions.
Learning how to use these powerful tools provides Linux users with two very valuable advantages. The first (and most immediate) of these is a time-saving way to process files and output from other commands.
The second is familiarity with regular expressions. Regular expressions are used throughout the Unix world in tools such as find and sed and languages such as awk, perl and Tcl. Learning this syntax prepares you to use some of the most powerful computing tools available.
Eric Goedelbecker is a systems analyst for Reuters America, Inc. He supports clients (mostly financial institutions) who use market data retrieval and manipulation APIs in trading rooms and back office operations. In his spare time (about 15 minutes a week...), he reads about philosophy and hacks around with Linux. He can be reached via e-mail at firstname.lastname@example.org.
|Android Candy: That App Is for the Birds!||Mar 22, 2017|
|Hodge Podge||Mar 21, 2017|
|William Rothwell and Nick Garner's Certified Ethical Hacker Complete Video Course (Pearson IT Certification)||Mar 20, 2017|
|Preseeding Full Disk Encryption||Mar 16, 2017|
|William Gurstelle's ReMaking History, Volume 3 (Maker Media, Inc.)||Mar 15, 2017|
|GRUB Boot from ISO||Mar 14, 2017|
- William Rothwell and Nick Garner's Certified Ethical Hacker Complete Video Course (Pearson IT Certification)
- Hodge Podge
- Two Ways GDPR Will Change Your Data Storage Solution
- Android Candy: That App Is for the Birds!
- Preseeding Full Disk Encryption
- Returning Values from Bash Functions
- William Gurstelle's ReMaking History, Volume 3 (Maker Media, Inc.)
- GRUB Boot from ISO
- Minifree Ltd.'s GNU+Linux Computers
- Flash ROMs with a Raspberry Pi
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