![]() General help using GNU software: Conclusion If any error occurs and -q is not given, the exit status is 2. With fewer than two FILEs, assume -h.Įxit status is 0 if any line is selected, 1 otherwise U, -binary do not strip CR characters at EOL (MSDOS/Windows) colour use markers to highlight the matching strings ![]() no-group-separator use empty string as a group separator group-separator=SEP use SEP as a group separator C, -context=NUM print NUM lines of output context A, -after-context=NUM print NUM lines of trailing context B, -before-context=NUM print NUM lines of leading context T, -initial-tab make tabs line up (if needed) c, -count print only a count of selected lines per FILE l, -files-with-matches print only names of FILEs with selected lines L, -files-without-match print only names of FILEs with no selected lines exclude-dir=PATTERN directories that match PATTERN will be skipped. exclude-from=FILE skip files matching any file pattern from FILE Skip files and directories matching FILE_PATTERN Search only files that match FILE_PATTERN D, -devices=ACTION how to handle devices, FIFOs and sockets d, -directories=ACTION how to handle directories I equivalent to -binary-files=without-match a, -text equivalent to -binary-files=text TYPE is 'binary', 'text', or 'without-match' binary-files=TYPE assume that binary files are TYPE q, -quiet, -silent suppress all normal output o, -only-matching show only the part of a line matching PATTERN label=LABEL use LABEL as the standard input file name prefix h, -no-filename suppress the file name prefix on output H, -with-filename print file name with output lines line-buffered flush output on every line n, -line-number print line number with output lines b, -byte-offset print the byte offset with output lines m, -max-count=NUM stop after NUM selected lines V, -version display version information and exit v, -invert-match select non-matching lines z, -null-data a data line ends in 0 byte, not newline x, -line-regexp force PATTERN to match only whole lines w, -word-regexp force PATTERN to match only whole words i, -ignore-case ignore case distinctions e, -regexp=PATTERN use PATTERN for matching P, -perl-regexp PATTERN is a Perl regular expression G, -basic-regexp PATTERN is a basic regular expression (default) F, -fixed-strings PATTERN is a set of newline-separated strings E, -extended-regexp PATTERN is an extended regular expression Įxample: grep -i 'hello world' menu.h main.c If you want to get more detailed usage info about the grep command, and you can pass the “ –help” option to the grep command to output a useage message, type: Let’s see the second field from the above outputs, and it indicates the line number of matching line within its input file (/etc/passwd, or /etc/group). etc/passwd:10:operator:x:11:0:operator:/root:/sbin/nologin Outputs: ~]# grep -n "root" /etc/passwd /etc/group If you want to add prefix in each line of output with the 1-based line number within its input file when using grep command, and you need to pass the -n or –line-number option to the grep command, type: $ grep -n "root" /etc/passwd /etc/group Outputs: ~]# grep -l "root" /etc/passwd /etc/group If you only want to dispaly file names when using grep command to searching mathcing lines from the given files or directories in your Linux system, and you need to pass the “ -l ” option to the grep command, type: $ grep -l "pattern" fileNames etc/passwd:operator:x:11:0:operator:/ root:/sbin/nologinįorm the above outputs, you should know that the first field is the file name that contains the mathcing line. Outputs: ~]# grep "root" /etc/passwd /etc/group /etc/passwd: root:x:0:0: root:/ root:/bin/bash The syntax of the grep command is as followss: grep PATTERN įor example, you want to find a pattern “ root” from two given files “ /etc/passwd” and “ /etc/group” with grep command, just type: $ grep "root" /etc/passwd /etc/group
0 Comments
Leave a Reply. |