当前位置:网站首页>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


边栏推荐
- 局域网SDN硬核技术内幕 25 展望未来——RDMA(下)
- 日本福岛废堆安全监视协议会认可排海计划“安全”
- EfficientNet
- Beyond compare 3 next difference segment / down search arrow not found
- 82. (cesium home) cesium points move on 3D models
- WGet warning: unable to verify
- 检定和校准的区别
- PWM的原理和PWM波的产生
- Error encountered in adding quick open option to right-click menu:
- 剑指 Offer 笔记: T58 - II. 左旋转字符串
猜你喜欢

第12章 泛型

82. (cesium home) cesium points move on 3D models

第8章 多线程

Maker Hongmeng application development training notes 02

Shell编程之正则表达式(Shell脚本文本三剑客之grep)

Analysis of distributed database and cache double write consistency scheme (Reprint)

Moveit2 - 4. robot model and robot state

VSCode复制代码时去掉样式/语法高亮/代码高亮/黑色背景

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

Adobe audit prompts that the sampling rate of audio input does not match the output device - problem solving
随机推荐
LeetCode 03: T58. 最后一个单词的长度(简单); 剑指 Offer 05. 替换空格(简单); 剑指 Offer 58 - II. 左旋转字符串(简单)
Everything cannot be searched for startup_ Lpc11x.s file
Moveit2 -- 2. Quick start of moveit in rviz
第7章 异常处理
【机器学习-白板推导系列】学习笔记---条件随机场
[machine learning whiteboard derivation series] learning notes - support vector machine and principal component analysis
Maker paper data search training notes
C programming language (2nd Edition) -- Reading Notes -- 1.5.2
Maker Hongmeng application development training notes 02
LeetCode 04: T26. 删除排序数组中的重复项(简单); 剑指 Offer 67. 把字符串转换成整数(中等); 面试题 01.08. 零矩阵 (简单)
STM32编译出现error: L6235E: More than one section matches selector - cannot all be FIRST/L
Modelarts voice detection and text classification
你真的会写二分查找吗——变种二分查找
Maker harmony OS application development training notes 01
Codeforces round #664C
Bus error problem of MMAP and its solution
多种进制之间的转换
Some commonly used shortcut keys for MathType
美现首例孕妇猴痘病例:新生儿被注射免疫球蛋白,已安全出生
C custom set