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 in Linux system

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use the grep command in the Linux system". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use the grep command in the Linux system.

Grep is one of the most commonly used tools for processing files in Linux systems. The most basic use of the grep command is to search for text characters or character sequences in files.

Text matching

The most basic use of the grep command is to search for text characters or sequences of characters in a file. For example, to display all lines in the / etc/passwd file that contain the string "bash", you need to run the following command:

[root@localhost ~] # grep bash / etc/passwd

Root:x:0:0:root:/root:/bin/bash

Bob:x:1000:1001::/home/bob:/bin/bash

User01:x:1001:1002::/home/user01:/bin/bash

By default, the grep command is case sensitive. This means that uppercase and lowercase characters are considered different. To ignore case when searching, use the-I option.

If the search string contains spaces, you need to enclose it in single or double quotes:

[root@localhost ~] # grep "System message bus" / etc/passwd

Dbus:x:81:81:System message bus:/:/sbin/nologin

Anchor point

The ^ symbol matches the empty string at the beginning of the line. In the following example, the string "root" matches only when the beginning of the line appears.

[root@localhost ~] # grep'^ root' / etc/passwd

Root:x:0:0:root:/root:/bin/bash

To find a line that ends with the string "bash", use the following command:

[root@localhost ~] # grep 'bash$' / etc/passwd

Root:x:0:0:root:/root:/bin/bash

Bob:x:1000:1001::/home/bob:/bin/bash

User01:x:1001:1002::/home/user01:/bin/bash

You can also use two anchors to construct regular expressions. For example, to view the configuration file without displaying blank lines, run the following command:

[root@localhost ~] # grep-v'^ $'/ etc/samba/smb.conf

-v reverses the meaning of the match to select rows that do not match.

| | symbol |

| Yes or yes. For example, you want to check whether cpu supports virtualization:

[root@localhost ~] # grep 'vmx\ | svm' / proc/cpuinfo

Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch ssbd ibrs ibpb stibp tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec arat md_clear spec_ctrl intel_stibp flush_l1d arch_capabilities

If you use extended regular expressions, you do not need to escape |, as follows:

[root@localhost ~] # grep-E 'svm | vmx' / proc/cpuinfo

Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch ssbd ibrs ibpb stibp tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec arat md_clear spec_ctrl intel_stibp flush_l1d arch_capabilities

At this point, I believe you have a deeper understanding of "how to use the grep command in the Linux system". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report