Home Web Front-end JS Tutorial Detailed explanation of the use of regex and Linux's three major text processing tools

Detailed explanation of the use of regex and Linux's three major text processing tools

Mar 29, 2018 pm 01:58 PM
linux deal with text

This time I will bring you a detailed explanation of the use of the three major text processing tools of Regex and Linux. What are the precautions when using the three major text processing tools of Regex and Linux. The following is a practical case. Let’s take a look. one time.

grep, sed and awk are all text processing tools. Although they are all text processing tools, they all have their own advantages and disadvantages. One text processing command cannot be completely replaced by another, otherwise it will not Three text processing commands appear.

1. Regular expression

1. Type of matching characters

[a-z]: Lowercase letters

[A-Z]: Uppercase letters

[a-Z]: Small or uppercase letters

[0-9]: Numbers

[a-zA-Z0-9]: Matches a character that is a letter or number

. : Matches any character, except spaces

[0-f] : Hexadecimal number

abc | def: abc or def
a (bc | de) f: abcf or adef

\<: The first word is usually preceded by a space or Special characters are used as separators, and continuous strings are treated as words

\>: end of words

[^expression]: except lowercase letters All characters except , and so on.

2, followed by the following symbols to control the number of matches

The left side of such symbols must have the expression of the first point above

expression *: 0 or n characters

expression +: 1 or n characters

expression?: 0 or 1 character

expression {n}: n characters

Expression{n:m}:n to m characters

Expression{n,}:at least n characters

[Example][a-z] * means matching 0 or more lowercase letters

3. Control the matching characters at the beginning and end
^ Expression: The head matches

Expression$: The tail matches

2. Three major text processing tools in Linux

1. egrep filtering tool

Extension of grep version, you can use regular expressions

Syntax:

egrep -option 'regular expression' file name

Options:

-n: display lines No.
-o: Only display matching content
-q: Silent mode, no output, you have to use $? to determine whether the execution is successful, that is, whether the desired content is filtered
-l: If the match is successful, only the file name will be printed. If it fails, it will not be printed. Usually -rl is used together. grep -rl 'root' /etc
-A: If the match is successful, the matching line and the following n lines will be printed. Print them out together
-B: If the match is successful, print out the matching line and the n lines before and after it.
-C: If the match is successful, print out the matching line and the n lines before and after it.
--color
-c: If the match is successful, print out the number of matched lines
-i: Ignore case
-v: Negate, do not match
-w: Match words

2. sed streamEditor

Syntax:

Syntax 1: sed - option 'Number positioning + command' File name

Options:

-n: Silent mode, no output
-e: Multiple edits, this is not very clear
-i: Directly modify the file content, and Not output
-r: Extended mode, you can use regular expressions
-f: Specify the file name and write the action in a new file

Command:

a: Append,
c: Change,
d: Delete,
i: Insert, i can be followed by strings, and these strings will appear in a new line (the current previous line )
p: Print print
s: Replace substitute, you can directly perform the replacement work. Usually this s action can be paired with a regular expression. For example, 1,20s/old/new/g

*s command special instructions:

Use {Command 1: Command 2: Command 3} to add multiple commands

语法2:sed  -r  '替换命令s/正则表达式/替换内容/贪婪选项g'  文件名

定位的两种方法:

①数字定位(输入行序号定位)

十进制数
1:单行
1,3:范围 从第一行到第三行
2,+4:匹配行后若干行
4,~3:从第四行到下一个3的倍数行
2~3:第二行起每间隔三行的行
$:尾行
1!:除了第一行以外的行

【例】sed -n '1p' /etc/passwd

②正则表达式定位

正则必须用//包裹起来

扩展正则需要用 -r 参数或转义

替换可使用正则表达式的子模式,即小括号(),可以\1、\2代表子模式

【例】sed -r 's/(.)(.)/\2\1/ file1 表示将匹配到的第一部分和第二部分替换

*贪婪选项:填上g,代表把一行中所有匹配项替换

3、awk 文本分析工具

由命令、正则(需要用//包围起来)、比较和关系运算组合而成

使用option中的-F参数定义间隔符号

用$1,$2,$3等的顺序表示files中每行以间隔符号分隔的各列不同域,NF变量表示当前记录的字段数

语法

awk -选项 参数  '逻辑判断{命令 变量1,变量2,变量3}' 文件名

选项

-F 定义字段分隔符,默认的分隔符是连续的空格或制表符
-v 定义变量并赋值 也可以借用次方式从shell变量中引入

AWK变量

NR 当前记录的个数(全部文件连接后的统计)
FNR 当前记录的个数(仅为当前文件的统计,非全部)
FS 字段分隔符 默认为连续空格或制表符,可以使用多个不同的符号做分隔符 -F[:/]
OFS 输出字符的分隔符 默认是空格
# awk -F: 'OFS="=====" {print $1,$2}' /etc/passwd
root=====x
NF 当前读入行的字段个数
ORS 输出记录分隔符 默认是换行
# awk -F: 'ORS="=====" {print $1,$2}' /etc/passwd
root x=====bin x=====
FILENAME 当前文件名

【例1】使用AWK变量

# awk '{print NR,FNR,$1}' file1 file2 
1 1 aaaaa
2 2 bbbbb
3 3 ccccc
4 1 dddddd
5 2 eeeeee
6 3 ffffff
#
Copy after login

【例2】引用shell变量的方法

# a=root
# awk -v var=$a -F: '$1 == var {print $0}' /etc/passwd
或者 把整个命令拆开传递,让shell变量外露,
# awk -F: '$1 == "'$a'" {print $0}' /etc/passwd
# a=NF
# awk -F: '{print $'$a'}' /etc/passwd
Copy after login

逻辑运算(可直接引用域进行运算)

= += -= /= *= 赋值

&& || ! 逻辑与 逻辑或 逻辑非

~ !~  匹配正则或不匹配,正则需要用 /正则/ 包围住
< <= > >= != ==  关系 比较字符串时要把字符串用双引号引起来

$ 字段引用:字段引用需要加$,而变量引用直接用变量名

+ - * / % ++ --  运算符

转义序列

\\ \自身
\$ 转义$
\t 制表符
\b 退格符
\r 回车符
\n 换行符
\c 取消换行

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

Linux grep与正则表达式的使用

怎么用正则把字符串分组

The above is the detailed content of Detailed explanation of the use of regex and Linux's three major text processing tools. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

vscode terminal command cannot be used vscode terminal command cannot be used Apr 15, 2025 pm 10:03 PM

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)

See all articles