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

Introduction of regular expressions in grep

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces the knowledge of "introduction to regular expressions in grep". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Grep is one of the tools used to process files in Linux. Grep searches the input file, finds the lines that match the regular expression, and outputs the standard output of each matching line.

A regular expression is a pattern that matches a set of strings. The pattern consists of operators, construction text characters, and metacharacters with special meaning. Grep supports three regular expression grammars: Basic, Extended, and perl compatibility.

If no regular expression type is provided, grep interprets the search pattern as a basic regular expression. To interpret the pattern as an extended regular expression, use-E.

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/passwdroot:x:0:0:root:/root:/bin/bashbob:x:1000:1001::/home/bob:/bin/bashuser01: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/passwddbus: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/passwdroot: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/passwdroot:x:0:0:root:/root:/bin/bashbob:x:1000:1001::/home/bob:/bin/bashuser01: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

Https://article.pchome.net/content-2112034.html

Https://www.51cto.com/it/news/2020/0525/20939.html

Https://www.csdn.net/article/a/2020-05-20/15991626

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

Summary

Regular expressions are used in text editors, programming languages, and command-line tools such as grep, sed, and awk. When searching for text files, writing scripts, or filtering command output, it is useful to understand how to construct regular expressions.

This is the end of the introduction to regular expressions in grep. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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