This is the 10th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

grep/egrep/fgrep

Grep, egrep, and fgrep: Search for files that meet specific patterns or strings, and output the results to 1 (stdout) without changing the original files.

(Patterns in UNIX are called Regular Expressions)

Grep =”Global, Regular Expressions, Print”

Grep, egrep can search for a specific Character Pattern (R.E.) in one or more files, etc.

grep

The grep command is used to search for specific patterns in each file or on standard output.

When using the grep command, each line containing a specified character pattern is printed on the screen. (Using the grep command does not change the contents of the file)

Grep Option mode file nameCopy the code
  • The grep option changes the way the grep command searches:
    • -c: Lists only the number of rows that contain the schema.
    • -i: Ignores the letter case in the mode.
    • -l: Lists the file names with matching rows.
    • -n: Lists the line number at the beginning of each line.
    • -v: Lists the rows that do not match the pattern.
    • -w: Searches the expression as a complete word, ignoring lines that partially match.

If multiple files are searched, the grep command displays only the file name whose matching pattern is found in the file. If a single file is searched, the grep command displays every line containing the matching pattern.

Note: Every option except the -w option can be used with the egrep and fgrep commands.


The grep command supports the following regular expression metacharacters (wildcards) :

  • c*: Matches zero (white space) or more than one character c (c is any character).
  • .: matches any character and only one character.
  • [xyz]: matches any character in the square brackets.
  • [^xyz]: matches all characters excluding those in square brackets.
  • ^: locks the beginning of a line.
  • $: Locks the end of the line.

In basic regular expressions, metacharacters *, +, {, |, (and) have lost their original meaning, if you want to restore their original meaning should be in before as backslash \, such as \ *, +, \ {\ \ |, \ (and \).

e.g.

$grep CLERK emp. Data 7369 SMITH CLERK 800 17-DEC-80 7876 ADAMS CLERK 1100 23-may-87 $grep -c CLERK Emp. data # lists all data lines starting with 78 in the emp.data file $grep '1.. 0' emp.data #'1.. 0' means a string that starts with a 1 followed by two arbitrary characters and ends with a 0. $ grep '[12].. 0 '#' [12].. 0' means a string that starts with 1 or 2 followed by two arbitrary characters and ends with 0. $grep -l root group passwd hosts # Search for the mode root in the group, passwd, and hosts files in the current directory and list the names containing the mode. $grep '/bash$' passwd # Lists all lines ending in /bash in the passwd file (Get all users who use bash by default). # $ps -e | grep FTP access To the system To use the FTP service process name $ps -e | grep ora # To know is Oracle Database runningCopy the code

Later additions:

In practice, I often use the following grep command:

grep -IRn PATTERN path/to/dir/
Copy the code
  • -I: Ignores binaries
  • -R: recursively searches all files in a directory and its subdirectories
  • -n: Lists the number of the matched line

This command is especially useful when searching the source code of a programming project, such as the definition of a function and its references:

$ grep -IRn "kinit" .
./kernel/defs.h:58:void            ramdiskinit(void);
./kernel/defs.h:65:void            kinit(void);
./kernel/ramdisk.c:16:ramdiskinit(void)
./kernel/main.c:19:    kinit();         // physical page allocator
./kernel/kalloc.c:27:kinit()
./kernel/kalloc.c:45:// initializing the allocator; see kinit above.)
Copy the code

egrep

Egrep is the alias of “grep -e” on Linux such as RHEL.

The syntax format of egrep is the same as the grep command, but egrep is used to search for specific patterns in the contents of one or more files using metacharacters of extended regular expressions.

Metacharacters added to egrep:

  • +: matches one or more leading characters.
  • a|b: Matches a or B.
  • (RE): Matches the regular expression RE in parentheses.

e.g.

$egrep '[1-5]+000' emp.data # $egrep '[a-z]+MAN' emp. FMT # $egrep '[a-z]+MAN' emp. FMT # $egrep '[1-5]+000' emp.data # $egrep '[a-z]+MAN' emp. XxMAN "$egrep. 'E | R (S)' emp FMT # in every line of figures in the search for the letter" E "followed by the S/RCopy the code

fgrep

Fgrep is the alias of “grep -f” on Linux such as RHEL

Fgrep is also used to search one or more files for lines that match a specified string.

Unlike grep, the fgrep command does not search for any regular expressions, that is, fgrep treats wildcards as normal characters (literally).

Fgrep can either type the pattern to search on the command line or use the -f option to read the pattern to search from a file.

Note: Searching the file fgrep is faster than grep, and fgrep can quickly search for multiple patterns at once.

e.g.

$cat emp. FMT | fgrep - the conditions f # is equivalent to $fgrep - the conditions f. TXT emp. FMTCopy the code