Grep command is also part of any beginner's UNIX command tutorial as it is an essential command to learn in order to work efficiently in any UNIX environment like Redhat Linux, Ubuntu, IBM AIX, Oracle Solaris, or BSD.Īnyway, these examples are by no means complete so please contribute you're own grep command tips or how you are using grep in Linux to make it more useful and allow all of us to benefit from each other's experience and work efficiently in UNIX or Linux.Įxample 1: How to ignore some words while doing a search using grep in UNIXįinding relevant words and exclusion of irrelevant words. The following examples of grep command in UNIX are based on my experience and I use them on a daily basis in my work. It's also very affordable and you can buy in just $10 on Udemy flash sales which happen every now and then.ġ0 ways to use Grep command in Unix - examples It's a very practical and hands-on course to learn Linux fundamentals in a quick time. If you need an online course, I highly recommend Linux Mastery: Master the Linux Command Line in 11.5 Hours on Udemy. Many people use grep just for finding words in a file and missed the real potential of grep by not using all its powerful command-line options and its regular expression capability which could not only save a lot of time but also works as a great and powerful tool while analyzing a large set of data or log files.īy the way, if you are new to Linux then I also suggest you go through a comprehensive Linux course to learn some basics commands and fundamentals like Linux file system, permissions, and other basic things. (bearing in mind that case-insensitive matching is often locale-dependent for instance, whether uppercase i is I or İ may depend on the locale according to grep -i i).This grep command tutorial is not about the theory of UNIX grep but the practical use of grep command in UNIX and here I am sharing my experience on the use of grep command in Linux with an aim that this would serve as a quick guide or tutorial for using grep in UNIX for new beginners and help them to understand the grep command better and its thoughtful usage in UNIX or Linux. The standard equivalent of that would look like: grep -e abc -e '' (and equivalent variants with other regexp syntaxes). Where it could be more interesting would be for instance if you want abc matching to be case sensitive and uyx not, which you could do with: grep -P 'abc|(?i)uyx' Those don't really bring much advantage over the standard -i option. Grep '\(?i\)abc|uyx' # ast-open grep only which makes it non-POSIX-compliant Grep -E '(?i)abc|uyx' # ast-open grep only Grep -K '~(i)abc|uyx' # ast-open grep only grep -P '(?i)abc|uyx' # wherever -P / -perl-regexp / -X perl is supported The (s and )s, like | also need to be quoted for them to be passed literally to grep as they are special characters in the syntax of the shell language.Ĭase insensitive matching can also be enabled as part of the regexp syntax with some grep implementations (not standardly). You can add (.)s around abc|uyx ( \(.\) for BREs), but that's not necessary. # can make it explicit with -G, -basic-regexp or # default but with some grep implementations, you Grep -i 'abc\|uyx' # with the \| extension to basic regexps supported by Grep -i -K 'abc|uyx' # ksh regexps (with ast-open grep) also with Grep -i -X 'abc|uyx' # augmented regexps (with ast-open grep) also with With some grep implementations, you can also do: grep -i -P 'abc|uyx' # perl-like regexps, sometimes also with Many options with grep alone, starting with the standard ones: grep -i -e abc -e uyx
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |