当前位置:网站首页>Regular expression of shell programming (grep of shell script text three swordsmen)
Regular expression of shell programming (grep of shell script text three swordsmen)
2022-07-27 11:46:00 【Brother frog who doesn't look back】
Catalog
1 Overview of regular expressions
1.1 Regular expression definition
1.1.1 Regular expressions make up
2 Basic regular expression metacharacters
1 Overview of regular expressions
Regular expressions , Also known as regular expression .(Regular Expression), In code it is often abbreviated as regex、regexp or RE. Regular expressions are often used for retrieval 、 Replace those that match a pattern ( The rules ) The text of .
There is more than one regular expression , and Linux Different programs in may use different regular expressions , Tools :grep、sed、awk、egrep
1.1 Regular expression definition
Regular expressions , Also called normal expression 、 Regular expressions
Use strings to describe 、 Match a series of strings that conform to a certain rule
1.1.1 Regular expressions make up
- Ordinary character : Case letters 、 Numbers 、 Punctuation and some other symbols
- Metacharacters : Special characters with special meaning in regular expressions
Metacharacters are special characters with special meaning in regular expressions , It can be used to specify its leading characters ( The character before the metacharacter ) The occurrence pattern in the target object
2 Basic regular expression metacharacters
| Common metacharacters | |
| \ | Escape character ,\!、\n etc. |
| ^ | Match the beginning of the string |
| $ | Match the end of the string |
. | Matching elimination \n Any character other than |
| * | Match the front face expression 0 Times or times |
| [ ] | Match a character in the list |
| [^ ] | Match any character that is not in the list ( Take the opposite ) |
| \{n,m\} | Match the previous subexpression n To m Time , Yes \{\n}、\{n,\}、\{n,m\} Three formats |
| + | Match the front face expression 1 More than once |
| ? | Match the front face expression 0 Time or 1 Time |
| () | Take the string in parentheses as a whole |
| | | Match a string of words by or |
3 Common pipeline commands
3.1 grep
grep [ Options ] … Search for conditions Target file
| Common options | |
| -E | Open extension (Extend) Regular expression of |
| -c | Calculate find “ Search string ” The number of times |
| -i | Ignore case differences , All cases are treated the same |
| -o | Only the string matched by the pattern |
| -v | Reverse selection ( Reverse lookup , Output lines that do not match the search criteria ) |
| -n | Sequential output line number |
example 1
Method 1

Method 2

example 2


example 3

example 4


example 5

example 6


example 7


3.2 cut( Column interceptor )
cut Command to cut bytes from each line of a file 、 Characters and fields and put these bytes 、 Write characters and strings to standard output
Format :cut [ Options ] Parameters
| Common options | |
| -b | Intercept by byte |
| -c | Intercept by character , Commonly used in Chinese |
| -d | Specify what to use as the delimiter to intercept , The default is tab |
| -f | Usually and -d Together |
example 1
Method 1

Method 2


example 2

example 3


3.3 sort
sort It is a tool to sort the contents of files in behavioral units
Format :sort [ Options ] Parameters
| Common options | |
| -t | Specify the separator , By default [Tab] Key or space division |
| -k | Specify sorting area |
| -n | Sort by number , The default is to sort in text |
| -u | Equate to uniq, Indicates that only one row of the same data is displayed .( Be careful : If there is a space at the end of the line, de duplication will not succeed ) |
| -r | Reverse sorting , The default is ascending ,-r It's in descending order |
| -o | Transfer the sorted results to the specified file |
example 1

example 2



example 3

3.4 uniq
uniq The command is used to check and delete the repeated rows and columns in the text file , Generally speaking, it is related to sort Command in combination with
Format :uniq [ Options ] Parameters
| Common options | |
| -c | Count the number of repeated rows |
| -d | Show only duplicate lines |
| -u | Show only rows that appear once |
example 1



example 2

3.5 tr
Commonly used to replace characters from standard input 、 Compress and delete .
Format :tr [ Options ] [ Parameters ]
| Common options | |
| -d | Delete character |
| -s | Delete all duplicate characters , Keep only one |
example 1

example 2

example 3


example 4

example 5


边栏推荐
- Codeforces round #664C
- 暂用 Solo,博客选择困难
- Keil MDK编译出现..\USER\stm32f10x.h(428): error: #67: expected a “}“错误的解决办法
- LAN SDN technology hard core insider 11 the key of cloud convergence CP -- hierarchical port binding
- 剑指 Offer 笔记: T45. 把数组排成最小的数
- C programming language (2nd Edition) -- Reading Notes -- 1.5.3
- 数据库 cli 工具 docker 镜像
- Quantitative industry knowledge summary
- 剑指 Offer 笔记: T57 - I. 和为 s 的两个数字
- Vscode removes style / syntax highlighting / code highlighting / black background when copying code
猜你喜欢

Shell脚本文本三剑客之sed

Detailed explanation of hash table

求不同采样周期下的传递函数有限零点

Analysis of the use of JUC framework from runnable to callable to futuretask

Matlab draws Bode diagram with time delay system

Everything cannot be searched for startup_ Lpc11x.s file

Error encountered in adding quick open option to right-click menu:

makefile模板

Maker Hongmeng application development training notes 02

The C programming language (2nd) -- Notes -- 1.6
随机推荐
Analysis of distributed database and cache double write consistency scheme (Reprint)
w.r.t. ; i.e.; etc.; e. G. what does it mean
LAN SDN technology hard core insider 11 the key of cloud convergence CP -- hierarchical port binding
Vscode establishes automatic search of header files under non engineering directories
The C programming language -- (2nd) -- Notes -- 4.11.2
Shell编程之正则表达式(Shell脚本文本三剑客之grep)
torch‘ has no attribute ‘inference_mode‘
【机器学习-白板推导系列】学习笔记---概率图模型和指数族分布
剑指 Offer 笔记: T57 - I. 和为 s 的两个数字
Principle of PWM and generation of PWM wave
LAN SDN technology hard core insider 12 cloud CP's daily love - hardware vxlan forwarding plane
Error encountered in adding quick open option to right-click menu:
Shell脚本文本三剑客之sed
Matlab S-function详解
(10) File contains
【机器学习-白板推导系列】学习笔记---支持向量机和主成分分析法
LeetCode 02: 剑指 Offer 58 - I. 翻转单词顺序(简单); T123. 验证回文串 ; T9. 回文数
剑指 Offer 笔记: T53 - I. 在排序数组中查找数字
EfficientNet
Redis simple to use