编程知识 cdmana.com

Regular expression in grep of Linux

grep yes Linux One of the tools used to process files in .grep Search input file , Find the row that matches the regular expression , And the standard output of each matching row is .

A regular expression is a pattern that matches a set of strings . Patterns are made up of operators 、 It is composed of literal characters and metacharacters with special meanings .grep Supports three kinds of regular expression syntax :Basic、Extended and perl compatible .

If no regular expression type is provided ,grep Interpret search patterns as basic regular expressions . To interpret patterns as extended regular expressions , Please use -E.

Text matching

grep command The most basic usage is to search for text characters or character sequences in a file . for example , To display the /etc/passwd The file contains the string “bash” All of the line , 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 ,grep Commands are case sensitive . This means that uppercase and lowercase characters are considered different . Ignore case when searching , Please use -i Options .

If the space contains a search string , It needs to be enclosed in single or double quotation marks :

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

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

Anchor point

^ The first line of an empty string matches . In the following example , character string “root” Match 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 string “bash” The line at the end , You can 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 , View the configuration file , Don't show blank lines , Please run the following command :

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


-v  Reverse the meaning of matching , To select mismatched lines .

| Symbol

|  Is or means . for example : Want to see cpu Is virtualization supported :

[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 , There is no need to escape |, As shown below :

[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

summary

Regular expressions are used in text editors 、 Programming languages and command line tools , Such as grep、sed and awk. Searching for text files 、 To write Script Or filter command output , It's useful to know how to construct regular expressions .

 

From the original :Linux It's time to learn

Link to the original text :https://www.linuxprobe.com/grep-command-pattern.html

版权声明
本文为[linuxprobe2020]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224085910238w.html

Scroll to Top