Grep для mac os

Grep для mac os

Understanding The «grep» Command In Mac OS X
Part XV of this series.
October 4th, 2002

I don’t know why Dudley keeps trying to find himself, I found him years ago.
Peter Cook

This series is designed to help you learn more about the Mac OS X command line. If you have any questions about what you read here, check out the earlier columns, write back in the comments below, or join us in the Hardcore X! forum.

In the previous column, we learned about regular expressions, and how to use them to search for text in vi. Having such a text-searching tool for the command line would be a valuable addition to Unix; naturally, such a tool exists. It is called grep, and it is the subject of today’s column.

grep allows you to search through your entire system, for either the name of a file, or for content within those files. This is similar to the way Sherlock used to work before Sherlock 3, and the way «Find» works today in Jaguar’s GUI. When you need to find a string of text on your system from the command line, grep is the way to do it. Now, on to how to use it.

The grep command will take a regular expression, as well as a list of files. It will then search through the files and, for each line that is matched by the regular expression, print the line. (Supposedly, the name grep comes from ed command g/RE/p, or «global/regular expression/print», which does the same thing within the editor. I can neither confirm nor deny this.) If there are no files indicated, grep will read from standard input. Therefore, you can do things like:

to give a more flexible search. Notice that the regular expression, .es.*, was enclosed in double quotes. Otherwise, we get this: [Note: I think that this is because the asterisk and/or period will confuse the tcsh command line, which tries to use them as metacharacers, so you need the quotes. On the other hand, if you want to anchor the regular expression to the end of a line with a dollar sign, it interprets this as a variable and chokes. tcsh is quirky with regular expressions, and I haven’t quite figured out everything with it. I know from experience that the Korn shell, ksh, does not suffer from this. On the other hand, ksh is not the default shell, so there y’are.]

You also need quotes if you have spaces in the regular expression. The difference between grep the file and grep «the » file is that the former will match any occurrence of t-h-e, whereas the latter will match only for t-h-e-space. This means that the former will match «I was there» but the latter will not. Remember that the command line ignores extra spaces, collapsing many into one, unless the spaces are quoted.

As you might expect, grep takes the standard regular expression characters of ., *, ^, $, \, and [ ]. Thus, to count the number of blank lines in a file, do:

Thus, we can see that grep ^$ testfile will print all three blank lines. We can use wc and the pipe, |, to build our own tool to count blank lines. Neat, huh?

In some Unixes (Unices?), there were two versions of grep, grep and egrep, whose primary difference was that each had slightly different additions to the basic regular expression syntax. In Darwin, and therefore in the syntaxes (syntaces?) are combined, and using either command will get you the same as using the other. Thus, you can bounce back and forth between them like so many yo-yos (yo-yi?)[*]

One set of regular expression characters available in grep is the \ < \>pair. This allows you to search for a range of occurrences. Suppose you want to look for «to», followed by three to nine characters, follow by an «a». This can be done by: Again, the quotes are needed here. If you want to match exactly 3, the regular expression is to.\<3\>a. Normally, the \ < \>pair is only available in grep, but in Darwin and it is also available in egrep.

Читайте также:  Как удалить windows 10 с компьютера mac

grep‘s regular expression syntax is expanded in to include features not seen in the standard definition of grep. In other words, grep will let you do searches that greps on other Unices won’t. For example, you can use the \ pair to denote the beginnings and endings of words, just like in vi.

We have seen that the asterisk (*) is used to denote «any number of the thing preceding me.» In grep, the plus sign, +, can be used to denote «at least on of the thing preceding me.» So, while the regular expression th*e will match te, the, thhe, . , the regular expression th+e will match the, thhe, thhhe, . . So can see that h+ is the same as hh*. The plus sign is often used in other utilities’ regular expressions, but is not part of grep on most other systems. Make a note of it, there will be a quiz later.

Another bonus freebie that is thrown our way is the question mark, ?, unless you are British and over 35, in which case it is «a mark of interrogation.» grep uses this in regular expressions to denote «zero or one occurrence of the thing before me», or «an optional [whatever is before me].» Therefore, the expression lie?d will match either lied or lid.

Finally, the vertical bar, |, can be used for either/or matching, just like in, you guessed it, vi.

grep can take several options; you can see them all via of course, but I’ve found that the most useful ones are (remember that this works in the grep option format):

-c: «count the lines». Instead of printing all the matched lines, -c merely prints a count of matched lines for each file. Thus that trick isn’t needed for one file. (If you pass in a list of files, though, . )

-e PATTERN: «expression starts here.» Using -e will tell grep «What follows is the pattern with which to search.» This is very useful when your pattern starts with a ‘-‘. Otherwise, the command line might think that your expression is an option and get confused.

-f FILE: «file holds the expression». -f allows you to store a pattern in a file and tell grep «Yo, use this.» I’ve mostly used this when writing scripts that will use the same pattern repeatedly. That way, if I have to change it later, I only have to change it in one place.

-i: «ignore case». -i forces grep to ignore the distinction between uppercase and lowercase. Imagine you need to find matches in a file which may have come from Windows (include shudder here). Now imagine a long string of paired letters like and on and on. Just use -i instead and save yourself time and pain.

-l: «list files». Instead of printing the matched lines,when you use the -l option, grep will just print a list of the files which contain the expression. This is mostly used when you are doing something like in a directory with a lot of files or when you just want to know which files need (processing, editing, etc).

-n: «number». -n means that before each line of output, grep will print its line number within the file.

-v: «invert». -v instructs grep to print only those lines that don’t match the expression.

As you can see, grep is a very powerful tool. It can be used to quickly search files and to filter output on the command line. It does have a couple limitations, though. First, it is no speed demon. Building those regular expressions and parsing a lot of text in a flexible way takes resources, and that takes time. (Admittedly, these days, that isn’t much of an issue, but still, there it is.) Second, consider the following: you are working away, happy as a clam, and the boss says «Cyprian», if your name is Cyprian, «I just got a call from marketing, we need to change the search in all those voodoo scripts you wrote, and we need it in ten minutes.»

Читайте также:  Управление linux сервером через telegram

Now, you know and I know that you can look for the expression and search for it using \ after \ after \. But my lord, and your duke for that matter, who the heck would want to? Do you realize that you would look for (or something along those lines) and heaven forbid you should make the slightest mistake. If you’re like me, and I know I am, you’d think «Now dash it, there must be an easier way. Surely, in all the history of Unix, someone has had to face just such an emergency and written a grep-like tool to deal with this. Like that Cyprian chap, maybe.» Well, Cyprian has come through. It’s called fgrep (for «fast grep»), and it works a lot like grep except it doesn’t take a regular expression.

Where you would normally place a regular expression, just put in a literal string. Originally it was used to be a fast alternative to grep by trading the power and flexibility of regular expressions for speed. As quick as computers are these days, that isn’t an issue, but if you want to find something that contains a literal period or a literal asterisk, it’s the bee’s knees.

[*] This joke was borrowed at great embarrassment from Shelley Berman. All young whippersnappers are advised to ask their parents or grandparents.

You are encouraged to send Richard your comments, or to post them below.

Most Recent Mac OS X Command Line 101 Columns

Mac OS X Command Line 101 Archives

Back to The Mac Observer For More Mac News!

Richard Burton is a longtime Unix programmer and a handsome brute. He spends his spare time yelling at the television during Colts and Pacers games, writing politically incorrect short stories, and trying to shoot the neighbor’s cat (not really) nesting in his garage. He can be seen running roughshod over the TMO forums under the alias tbone1.

We also offer Yesterday’s News On One Page!

Mac Products Guide
New Arrivals
New and updated products added to the Guide.

Hot Deals
Great prices on hot selling Mac products from your favorite Macintosh resellers.

Special Offers
Promotions and offers direct from Macintosh developers and magazines.

Software
Browse the software section for over 17,000 Macintosh applications and software titles.

Hardware
Over 4,000 peripherals and accessories such as cameras, printers, scanners, keyboards, mice and more.

© All information presented on this site is copyrighted by The Mac Observer except where otherwise noted. No portion of this site may be copied without express written consent. Other sites are invited to link to any aspect of this site provided that all content is presented in its original form and is not placed within another .

Источник

How to install and use GNU Grep in macOS

I have coreutils but I am not sure if GNU grep is there. I just want to use the flag -P for Perl regex that is found in GNU grep, but not in BSD grep.

My PATH is /usr/local/opt/coreutils/libexec/gnubin:/usr/local/bin:/usr/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/usr/texbin:/Users/masi/.cabal/bin so I have coreutils first in the PATH.

However, grep is BSD when I am using it: grep —version gives grep (BSD grep) 2.5.1-FreeBSD .

Command type -p grep returns /usr/bin/grep

How can you install GNU Grep in macOS?

2 Answers 2

GNU grep is not part of coreutils. To install, run

As with coreutils, this doesn’t automatically replace the existing grep

So after installing you can either use ggrep , gegrep and gfgrep ; or extend PATH as shown above to use grep etc. The second option may confuse some macOS specific scripts though in case the options differ.

The answer from nohillside needs updating as follows:

If grep was already installed by brew, remove grep first.

Then install grep:

Note that you do need to modify the PATH. For example, add to your .bashrc:

I had to do the above on my mac when after brew upgrade my grep was no longer accessible (it was installed previously with brew install grep —with-default-names , and this option is not available any more).

This solution works as of Homebrew 2.1.1:

This answer is based on the one from nohillside, with comments from Jonathan Komar and scott m gardner.

Источник

macOS: Using “Grep” to Find Matching Lines

I may have mentioned a couple of times before that I enjoy using the built-in Terminal utility. A few years ago, I often found it useful for troubleshooting permissions problems, for example, and as TMO’s Jeff Butts points out, there are tons of under-the-hood changes you can make by knowing how to use certain commands. Or heck, by copying and pasting them in from the Internet. The Terminal doesn’t care if you know what you’re doing, which is why it’s always a great idea to be careful what you type in it! That’s my PSA for today, folks.

Anyway, one command that’s simple to use (at least at its most basic level) yet handy is grep . As Terminal’s manual page on it explains:

What this means in more simple terms is that you can use grep to pull lines that contain search terms out of a text file. Here’s how it works: Let’s pretend this text file of mine has many hundreds of lines of data that I need to paw through.

Boy, I am just stupidly bad at coming up with random words.

If what I need to do is take all of the lines containing “test” and separate them from the lines containing everything else, that’s really simple for grep . What I’ll do first is open Terminal, of course—it lives in my Applications > Utilities folder—and after it gives me a prompt to start (ending with a dollar sign), I’ll type this:

So I’ve entered the grep command followed by my search term—“test”—and now I just have to tell Terminal which file to run things on. An easy way I can do this is by making sure to type a space after my search term, and then I’ll drag and drop the file I want to search onto the Terminal window, like so:

Once I do that, the program’ll fill in the path to the file for me, easy-peasy.

If I then press Return on my keyboard, the Terminal window will fill up with the lines that match my search! Neat!

When you get to this point with your own grepping, which is probably a word I just made up, you can copy and paste the resulting data out of Terminal. Or if you’re more fancy, you could use a right-angle bracket (“>”) to “pipe” the info out into a new file by adding on to your command, like this:

You have to know a bit about how to use file paths to really understand what you’re doing here, but in plain English, my command above says “find lines matching ‘test’ in the ‘Grep.txt’ document on Melissa’s desktop, then put the resulting data into a file called ‘testfile.txt,’ also on her desktop.” Be careful, though, that you aren’t pointing Terminal to an existing file! If you already have a “testfile.txt” on your desktop, this command will overwrite it.

Finally, one more detail—if your search term contains any special characters (such as spaces, say), you’ll have to put a backslash in front of them to make Terminal interpret them correctly. So for example, if you wanted to find lines containing “scary sewer clown,” you’d type this:

grep scary\ sewer\ clown /Path/To/File

The g rep command is actually incredibly powerful, and some of its functionality is admittedly way over my head. Still, even peons like me can use it to do simple line-searches! If you’re braver than I and would like to delve into it more deeply, just open Terminal and type man grep at the prompt to read more of its manual page. And let me know if you use it for anything fun!

Источник

Читайте также:  Linux сборка своими руками
Оцените статью