Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the grep command under Linux

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use grep commands under Linux". In daily operation, I believe many people have doubts about how to use grep commands under Linux. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use grep commands under Linux". Next, please follow the editor to study!

The grep command is used to search for text, or to search a given file for a file that contains a given string or word on a line. In general, grep displays the rows that match. Use grep to search for lines of text that include one or more regular expressions that match, and then display only the lines that match. Grep is considered to be one of the most useful commands in Linux/ Unix systems.

The name grep comes from a command that performs a similar operation in ed, an ancient line editor in Unix/Linux:

The code is as follows:

G/re/p

Syntax of the grep command

The syntax is as follows:

Grep 'word' filename

Grep 'word' File 1 File 2 File 3

Grep 'string 1 string 2' file name

Cat a file | grep 'something'

Command | grep 'something'

Command option 1 | grep 'data'

Grep-color 'data' file name

How to use grep to search for a file

Search for boo users under the / etc/passwd file, enter:

The code is as follows:

$grep boo / etc/passwd

Output:

The code is as follows:

Foo:x:1000:1000:foo,:/home/foo:/bin/ksh

You can use grep to force case ignorance. For example, use the-I option to match boo, Boo, BOO, and other combinations:

The code is as follows:

$grep-I "boo" / etc/passwd

Recursively use grep

You can search recursively using grep. For example, search under the file directory for all files that contain the string "192.168.1.5"

The code is as follows:

$grep-r "192.168.1.5" / etc/

Or:

The code is as follows:

$grep-R "192.168.1.5" / etc/

Sample output:

The code is as follows:

/ etc/ppp/options:# ms-wins 192.168.1.50

/ etc/ppp/options:# ms-wins 192.168.1.51

/ etc/NetworkManager/system-connections/Wired connection 1Plus addresses1vendor 192.168.1.5 Ten 24tern 192.168.1.2

You will see that every line of the search to 192.168.1.5 is prefixed to find a matching file name (for example: / etc/ppp/options). The file name contained in the output can be suppressed by adding the-h option:

The code is as follows:

$grep-h-R "192.168.1.5" / etc/

Or

The code is as follows:

$grep-hR "192.168.1.5" / etc/

Sample output:

The code is as follows:

# ms-wins 192.168.1.50

# ms-wins 192.168.1.51

Addresses1=192.168.1.5;24;192.168.1.2

Use grep to search for text

When you search for boo, the grep command will match fooboo,boo123, barfoo35, and all other strings containing boo. You can use the-w option to force only those lines that contain only that entire word. (LCTT translation note: that is, the string is flanked by English word separators, such as spaces, punctuation, and the end, etc., so it does not apply to the Chinese language without hyphenation. ).

The code is as follows:

$grep-w "boo" file

Use the grep command to search for two different words

Use the egrep command as follows:

The code is as follows:

$egrep-w 'word1 | word2' / path/to/file

(note: regular expressions are used here, so the egrep command, the extended grep command, is used. )

Count the number of lines to which the text matches

The grep command can display the number of matches in each file by adding the-c argument:

The code is as follows:

$grep-c 'word' / path/to/file

Pass the line number of the matching line before the output line with the-n option:

The code is as follows:

$grep-n 'root' / etc/passwd

Sample output:

The code is as follows:

1:root:x:0:0:root:/root:/bin/bash

1042:rootdoor:x:0:0:rootdoor:/home/rootdoor:/bin/csh

3319:initrootapp:x:0:0:initrootapp:/home/initroot:/bin/ksh

Reverse match (mismatch)

You can use the-v option to output content that does not contain a match, and the output contains only those lines that do not contain a given word, such as all lines that do not contain bar words:

The code is as follows:

$grep-v bar / path/to/file

UNIX/Linux pipes and grep commands

Grep is often used with pipes, and in this case, the name of the hard disk device is displayed:

The code is as follows:

# dmesg | egrep'(s | h) d [amurz]'

Display the CPU model number:

The code is as follows:

# cat / proc/cpuinfo | grep-I 'Model'

However, the above command can also be used in the following ways, without piping:

The code is as follows:

# grep-I 'Model' / proc/cpuinfo

Sample output:

The code is as follows:

Model: 30

Model name: Intel (R) Core (TM) i7 CPU Q 820 @ 1.73GHz

Model: 30

Model name: Intel (R) Core (TM) i7 CPU Q 820 @ 1.73GHz

How to display only the name of the file that matches the content?

Use the-l option to display the file names that contain main () in the contents of the file:

The code is as follows:

$grep-l 'main' * .c

Finally, you can force grep to output in color:

The code is as follows:

$grep-- color vivek / etc/passwd

Sample output:

At this point, the study on "how to use the grep command under Linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report