Linux Command grep, linuxgrep
Linux Command grep
The grep command is used to search for text, or to search for a file containing a given string or word in a row from a given file. In general, grep displays matched rows. Use grep to search for text rows that match one or more regular expressions, and then show only matched rows.
Grep command syntax:
Grep 'word' file name
Grep 'word' file 1 file 2 file 3
Grep 'string 1 string 2' file name
Commad | grep 'something'
Commad Option 1 | grep 'data'
Grep -- color 'data' file name
Use grep to search for a file
Search for files
Search for Linux users in the/etc/passwd file and enter:
[Root @ centos-64-min ~] # Grep Linux/etc/passwd
Output content:
Linux: x: 500: 500:/home/Linux:/bin/bash
You can use grep to forcibly ignore case sensitivity. For example, you can use the-I option to match linux, Linux, and other combinations:
Grep-I Linux/etc/passwd
Recursive grep
You can use grep recursively for search. For example, search for all files containing the string "192.168.57.9" in the file directory.
[Root @ centos-64-min ~] # Grep-r "192.168.57.9"/etc/
Or
[Root @ centos-64-min ~] # Grep-R "192.168.57.9"/etc/
Output result
/Etc/sysconfig/network-scripts/ifcfg-eth0: IPADDR = 192.168.57.9
You will see that the search results of 192.168.57.9 have a prefix for each row to find the matching file name. You can add the-h option to the file name contained in the output to disable the output:
For example:
[Root @ centos-64-min ~] # Grep-hR "192.168.57.9"/etc/
IPADDR = 192.168.57.9
Or
[Root @ centos-64-min ~] # Grep-h-R "192.168.57.9"/etc/
IPADDR = 192.168.57.9
Use grep to search for text
When you search for Linux, The grep command matches Linux, linux123, Linux35, and other Linux strings, you can use the-w option to force only the rows containing the entire word to be output.
[Root @ centos-64-min ~] # Grep-w linux/etc/passwd
Use the grep command to search for two different words
The egrep command is as follows:
Egrep-w 'word1 | word2 ', path/to/file
The regular expression is used here, so the egrep command is used, that is, the extended grep command.
Count the number of lines matching text
The grep command can display the number of matching times in each file by adding the-c parameter:
Grep-c 'word'/path/to/file
The "-n" option can be used to output the line number of the matched line before the flight:
Grep-n 'root'/etc/passwd
For example:
[Root @ centos-64-min ~] # Grep-n 'root'/etc/passwd
1: root: x: 0: 0: root:/bin/bash
11: operator: x: 11: 0: operator:/root:/sbin/nologin
Reverse match
Use the-v option to output the content that does not contain the matching item. The output content only contains rows that do not contain the given word. For example, output all rows that do not contain the root word.
[Root @ centos-64-min ~] # Grep-n 'root'/etc/passwd
1: root: x: 0: 0: root:/bin/bash
11: operator: x: 11: 0: operator:/root:/sbin/nologin
[Root @ centos-64-min ~] # Grep-n "root"/etc/passwd
1: root: x: 0: 0: root:/bin/bash
11: operator: x: 11: 0: operator:/root:/sbin/nologin
[Root @ centos-64-min ~] # Grep-v "root"/etc/passwd
Bin: x: 1: 1: bin:/sbin/nologin
Daemon: x: 2: 2: daemon:/sbin/nologin
Adm: x: 3: 4: adm:/var/adm:/sbin/nologin
Lp: x: 4: 7: lp:/var/spool/lpd:/sbin/nologin
Sync: x: 5: 0: sync:/sbin:/bin/sync
Shutdown: x: 6: 0: shutdown:/sbin/shutdown
Halt: x: 7: 0: halt:/sbin/halt
Mail: x: 8: 12: mail:/var/spool/mail:/sbin/nologin
Uucp: x: 10: 14: uucp:/var/spool/uucp:/sbin/nologin
Games: x: 12: 100: games:/usr/games:/sbin/nologin
Gopher: x: 13: 30: gopher:/var/gopher:/sbin/nologin
Ftp: x: 14: 50: FTP User:/var/ftp:/sbin/nologin
Nobody: x: 99: 99: Nobody: // sbin/nologin
Vcsa: x: 69: 69: virtual console memory owner:/dev:/sbin/nologin
Saslauth: x: 499: 76: "Saslauthd user":/var/empty/saslauth:/sbin/nologin
Postfix: x: 89: 89:/var/spool/postfix:/sbin/nologin
Sshd: x: 74: 74: Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
Linux: x: 500: 500:/home/Linux:/bin/bash
Pipeline and grep command
Grep is often used with pipelines.
If the name of the disk device is displayed:
[Root @ centos-64-min ~] # Dmesg | egrep '(s | h) d [a-z]'
NMI watchdog disabled (cpu0): hardware events not enabled
Sd 2: 0: 0: 0: [sda] 41943040 512-byte logical blocks: (21.4 GB/20.0 GiB)
Sd 2: 0: 0: 0: [sda] Write Protect is off
Sd 2: 0: 0: 0: [sda] Mode Sense: 61 00 00 00
Sd 2: 0: 0: 0: [sda] Cache data unavailable
Sd 2: 0: 0: 0: [sda] Assuming drive cache: write through
Sd 2: 0: 0: 0: [sda] Cache data unavailable
Sd 2: 0: 0: 0: [sda] Assuming drive cache: write through
Sda: sda1 sda2
Sd 2: 0: 0: 0: [sda] Cache data unavailable
Sd 2: 0: 0: 0: [sda] Assuming drive cache: write through
Sd 2: 0: 0: 0: [sda] Attached SCSI disk
Dracut: Scanning devices sda2 for LVM logical volumes vg_centos64min/lv_root vg_centos64min/lv_swap
EXT4-fs (sda1): mounted filesystem with ordered data mode. Opts:
SELinux: initialized (dev sda1, type ext4), uses xattr
Display the CPU model:
[Root @ centos-64-min ~] # Cat/proc/cpuinfo | grep-I "Model"
Model: 42
Model name: Intel (R) Core (TM) i5-2450M CPU @ 2.50 GHz
[Root @ centos-64-min ~] #
Of course, pipelines are not applicable.
[Root @ centos-64-min ~] # Grep-I 'model'/proc/cpuinfo
Model: 42
Model name: Intel (R) Core (TM) i5-2450M CPU @ 2.50 GHz
[Root @ centos-64-min ~] # Grep 'model'/proc/cpuinfo
[Root @ centos-64-min ~] # Grep 'model'/proc/cpuinfo
Model: 42
Model name: Intel (R) Core (TM) i5-2450M CPU @ 2.50 GHz
Only display the name of the file matching the content
Grep-l 'main' *. c
Alternatively, you can force grep to output in color:
Grep -- color vivek/etc/passwd