Matches all “graphical” characters (all letters, numbers, and punctuation marks)Īnd if you start your character class with the special ^ character, it takes on a new behavior: you can instead match all those lines which contain characters not matching your character set. Matches all numbers and letters (in the language of your current locale) Grep also supports the matching of “character classes” - displaying a line if it contains any one of the characters indicated in brackets.īut there’s also several handy pre-defined character sets. Grep '^Only this text appears on the line$' filenameīecause of ^ and $, there’s also an easy way to search for blank lines. Of course, there’s an alternate way to do that, using grep‘s ^ and $ metacharacters, which let you match the beginning and end of a line, respectively. Grep -x 'Only this text appears on the line' filename For example, if you’re looking for the word “ant” but don’t want to match all the other words which *contain* ant:Īnd if you want to only display matches when your search string is the entire line, try -x: If your grep searches are giving you a lot of “false positives,” there’s a built-in way to only display a match when your search pattern is a complete word: the -w flag. For example, grep uses a dollar sign as a special character matching the end of a line - so if you actually want to search for a dollar sign, you have to precede it by a backslash (and include the whole search string in single quotes).īut fgrep lets you just type in that dollar sign.Īnd you can also use fgrep to match for a dot or a caret without having to precede it with a backslash - which does make things more readable. Again it warns that fgrep “is deprecated” but “provided to allow historical applications that rely on them to run unmodified.” I’ve heard it referred to as “ fast grep,” because it basically throws away all of grep’s regular expression-matching, and just concentrates on quickly looking only for matching strings. Grep also spawned yet-another standalone tool, fgrep, which grep’s man page explains is the same as grep -F. You can also create a group of characters - which will only be considered a match if the entire group is present. Again, grep requires that you precede the pipe character with a backslash - but with egrep you can simply include it in your commands. The “pipe” character lets you separate a pair of characters, and will match if a line contains either character. You can also do the same quantity-matching with grep, but you have to precede the characters with a backslash.Īnd there’s one more powerful way to soup up your pattern matching: alternation. That lets you get more specific about how many times you want a character to appear in your matches. It’s like grep with a superpower - it searches through every subdirectory.Īnd then there’s egrep , which is the same as grep -E, though the grep man page warns that egrep “is deprecated,” but “is provided to allow historical applications that rely on them to run unmodified.” Searches with egrep match not only the usual metacharacters ( .* ^ $) but also the Posix-defined set of (E)xtended regular expressions. For example, there’s the recursive rgrep, which is the same as grep -r. History has it that Ken Thompson coded up the grep tool overnight to help a colleague search through the entire text of the Federalist Papers without having to load the whole thing into memory first.īut in the same way that the ed command g/ re/p became a stand-alone tool named “grep,” some of grep’s most useful flags eventually spun off into more tools. Yes, it was such a useful command that eventually became its own stand-alone tool.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |