做减肥餐的网站,网站优化注意事项,猪八戒网站怎么做任务,企业网站功能对比分析Linux运维-SHELL编程之正则表达式与流编辑处理器
什么是正则表达式
正则表达式是一种用来描述字符序列的强大工具#xff0c;通常用于字符串的匹配、搜索和替换操作。它由普通字符#xff08;例如字母、数字#xff09;和特殊字符#xff08;称为元字符#xff09;组成通常用于字符串的匹配、搜索和替换操作。它由普通字符例如字母、数字和特殊字符称为元字符组成通过这些元字符可以构建出具有特定模式的字符串。正则表达式在文本处理、数据提取、验证输入等方面都有广泛的应用。
我们可以理解为正则表达式是一种字符串的抽象代表符合某些要求的字符串的一个范式在一般情况下只要满足了正则表达式的条件就会被认定为匹配成功
正则表达式基本内容
基本元字符
元字符描述.匹配任意单个字符除了\n与\r^匹配字符串的开始$匹配字符串的结束*匹配前面的字符零次或多次匹配前面的字符至少一次或多次?匹配前面的字符零次或一次[]匹配括号内的所有字符可在内部字符串首写^表示不匹配给定字符例如[^ABC][-]匹配在连续范围内的字符[^]匹配不在该组内的字符()表示一个子表达式{n}匹配时指定前面字符出现的重复次数必须为n{n,}匹配时指定前面字符出现的重复次数至少为n也可以指定至多出现次数{n,m}\转义字符用于匹配特殊字符\匹配一个词组开头的字串 正则表达式的贪婪greedy与非贪婪non-greedy指的是匹配模式下的不同行为。 贪婪正则表达式默认是贪婪的即会尽可能匹配更多的字符。例如对于正则表达式a.*b如果用来匹配字符串aabb它会匹配整个字符串而不是只匹配到第一个b为止。非贪婪通过在量词后面加上?可以实现非贪婪匹配这样正则表达式会尽可能少的匹配字符。例如对于正则表达式a.*?b如果用来匹配字符串aabb它会匹配到第一个ab为止而不是匹配整个字符串。 在量词后面加上?可以使得量词变成非贪婪的否则默认是贪婪的。 带反斜杠的元字符
元字符描述\s用来匹配所有空白字符包括换行符\S用来匹配非空白符不包括换行符\w匹配字母数字下划线\d用来匹配数字
分组与捕获元
在正则表达式中分组和非捕获元是用来对子表达式进行分组或者控制匹配行为的元字符。 分组使用圆括号()来创建一个分组可以对其中的子表达式进行分组并对整个分组应用量词。例如(?:...)是一个非捕获分组不会捕获匹配的内容而(abc)是一个捕获分组会捕获匹配的内容以便后续使用。 非捕获元用于对子表达式进行分组但不捕获匹配的内容。它可以避免将匹配的内容存储在匹配结果中仅用于分组或者应用量词。
非捕获元捕获元描述(?:exp1)捕获 exp1但不记住匹配项。这在需要分组但不需要捕获匹配内容时很有用。(?exp1)匹配 exp1 前面的位置但不消费任何字符。这称为正向先行断言。例如(?\d) 匹配一个数字前面的位置但不匹配数字本身。(?!exp1)匹配后面跟的不是 exp1 的位置。这称为负向先行断言。例如(?!\d) 匹配后面不是数字的位置。(?exp1)匹配 exp1 后面的位置但不消费任何字符。这称为正向后行断言。例如(?\d) 匹配一个数字后面的位置但不匹配数字本身。(?!exp1)匹配前面不是 exp1 的位置。这称为负向后行断言。例如(?!\d) 匹配前面不是数字的位置。 可以通过索引来引用。在使用捕获元时可以通过\1、\2等来引用先前捕获的内容。 修饰符
修饰符描述i执行对大小写不敏感的匹配。m多行模式。改变 ^ 和 $ 的行为使它们分别在行的起始和结束处匹配而不是在整个输入字符串的起始和结束处匹配。s单行模式。改变 . 的行为使其匹配所有字符包括换行符 \n。g全局模式。查找所有匹配项而不是在找到第一个匹配项后停止。
正则表达式匹配实例
匹配一个有效的IPv4地址匹配形如192.168.1.1的IPv4地址
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$匹配一个日期YYYY-MM-DD匹配形如2024-04-01的日期
^\d{4}-\d{2}-\d{2}$匹配一个有效的邮箱地址匹配形如exampleexample.com的邮箱地址
^[a-zA-Z0-9._%-][a-zA-Z0-9.-]\.[a-zA-Z]{2,}$匹配一个简单的HTML标签不考虑属性匹配形如divcontent/div的简单HTML标签
[a-zA-Z][a-zA-Z0-9]*(.*?)\/[a-zA-Z][a-zA-Z0-9]*匹配一个简单的URL不考虑协议、端口和查询参数匹配形如http://example.com的简单URL
^(http|https):\/\/[a-zA-Z0-9\-\.]\.[a-zA-Z]{2,}(\/\S*)?$在之前的文章中我们介绍过[[...]]可以使用正则表达式 if [[ $string ~ pattern ]]; thenecho Matched
fi但是正则表达式使用更常见的地方并不在于此而是流编辑处理器 文本查找工具grep
grep 是一个强大的文本搜索工具常用于在文件中查找特定模式的文本行。以下是 grep 命令的一些常见用法 基本用法在文件中搜索指定模式的文本行。 grep pattern file.txt递归搜索在指定目录及其子目录中递归搜索匹配的文本行。 grep -r pattern directory/忽略大小写在搜索时忽略模式的大小写。 grep -i pattern file.txt显示匹配行数显示匹配到的文本行的行数。 grep -c pattern file.txt显示不匹配行显示不包含匹配模式的文本行。 grep -v pattern file.txt显示匹配文本仅显示匹配到的文本而不显示整行。 grep -o pattern file.txt显示匹配行及上下文显示匹配到的文本行及其上下文行。 grep -C 2 pattern file.txt # 显示匹配行及其上下各两行显示匹配行号显示匹配到的文本行的行号。 grep -n pattern file.txt限制搜索深度在递归搜索时限制搜索的深度。 grep --max-depth1 pattern directory/扩展正则当使用 -E 选项时grep 将以扩展的正则表达式语法进行匹配 grep -E pattern file.txt流编辑处理器sed
sed是一种在线的、非交互式的编辑器它一次处理一行内容。处理时把当前处理的行存储在临时缓冲区中称为模式空间patternspace。接着用sed命令处理缓冲区中的内容处理完成后把缓冲区的内容送往屏幕。接着处理下一行这样不断重复直到文件末尾。文件内容并没有改变除非使用重定向存储输出。Sed主要用来自动编辑一个或多个文件简化了对文件的反复操作。
sed使用模式与常用选项
sed的使用模式一般是如下状态
sed 选项 命令 文件选项描述-e script将指定的 script 用作 sed 命令。-f file从指定文件中读取 sed 脚本。-i suffix直接在文件中进行编辑并可选地备份原始文件备份文件的后缀名由 suffix 指定。-n不输出模式空间的内容只有通过命令显式指定打印内容时才会输出。-r 或 --regexp-extended使用扩展的正则表达式语法。-s 或 --separate将输入视为多个独立的文件。-u 或 --unbuffered使用无缓冲的输出。-V 或 --version显示 sed 的版本信息。 -e 和 -f 是用于指定 sed 脚本的选项但有一些区别 -e script: -e 选项允许在命令行上直接指定 sed 脚本可以多次使用 -e 选项每次指定一个脚本这些脚本将按照指定的顺序依次执行。例如 sed -e s/abc/def/ -e s/123/456/ input.txt这将依次执行两个替换操作第一个将 abc 替换为 def第二个将 123 替换为 456。 -f file: -f 选项允许从指定的文件中读取 sed 脚本。文件中的每一行都被视为一个 sed 命令。例如如果有一个名为 script.sed 的文件包含以下内容 s/abc/def/
s/123/456/可以像如下使用 -f 选项这将会按照文件中的顺序依次执行两个替换操作 sed -f script.sed input.txt我们接下来以如下文件来演示sed功能
Hello, World!
This is a test file for sed.
It contains some sample text that can be used to test various sed commands.
Feel free to modify this file and experiment with different sed commands.
sed is a powerful stream editor.
It can perform a wide range of text processing functions.
Enjoy testing sed!
sed删除文件中指定行
删除指令的基本格式如下d表示执行删除操作
sed 选项 /匹配条件/d 文件名注意为了防止在shell中出现冲突最好是使用强解析即避免造成命令歧义
删除符合正则匹配条件的行
r123localhost:~/shell_code$ sed -e /commands/d content.txt
Hello, World!
This is a test file for sed.
sed is a powerful stream editor.
It can perform a wide range of text processing functions.
Enjoy testing sed!删除指定行号的内容
r123localhost:~/shell_code$ sed -e 7d content.txt
Hello, World!
This is a test file for sed.
It contains some sample text that can be used to test various sed commands.
Feel free to modify this file and experiment with different sed commands.
sed is a powerful stream editor.
It can perform a wide range of text processing functions.删除指定行号范围内的行
r123localhost:~/shell_code$ sed -e 3,4d content.txt
Hello, World!
This is a test file for sed.
sed is a powerful stream editor.
It can perform a wide range of text processing functions.
Enjoy testing sed!注意这里的行号还可以用$来指定最后一行 r123localhost:~/shell_code$ sed -e $d content.txt
Hello, World!
This is a test file for sed.
It contains some sample text that can be used to test various sed commands.
Feel free to modify this file and experiment with different sed commands.
sed is a powerful stream editor.
It can perform a wide range of text processing functions.r123localhost:~/shell_code$ sed -e 3, $d content.txt
Hello, World!
This is a test file for sed.sed替换文件中的指定内容
替换命令的格式如下正则修饰符是可选的
sed 选项 s/匹配条件/用于替换的字符串/[正则修饰符] 文件名我们以将所有sed替换为SED
r123localhost:~/shell_code$ sed -e s/sed/SED/g content.txt
Hello, World!
This is a test file for SED.
It contains some sample text that can be uSED to test various SED commands.
Feel free to modify this file and experiment with different SED commands.
SED is a powerful stream editor.
It can perform a wide range of text processing functions.
Enjoy testing SED!/g表示全局匹配如果不加的话只会替换每行中的第一个sed r123localhost:~/shell_code$ sed -e s/sed/SED/ content.txt
...
It contains some sample text that can be uSED to test various sed commands.
...对匹配到的部分进行增加内容即使用表示匹配到的表达式
r123localhost:~/shell_code$ sed -e s/sed/SED/g content.txt
Hello, World!
This is a test file for sedSED.
It contains some sample text that can be usedSED to test various sedSED commands.
Feel free to modify this file and experiment with different sedSED commands.
sedSED is a powerful stream editor.
It can perform a wide range of text processing functions.
Enjoy testing sedSED!这种操作实质上是相当于使用了捕获元只不过使用捕获元必须在扩展正则状态下
r123localhost:~/shell_code$ sed -r s/(sed)/\1SED/g content.txt
Hello, World!
This is a test file for sedSED.
It contains some sample text that can be usedSED to test various sedSED commands.
Feel free to modify this file and experiment with different sedSED commands.
sedSED is a powerful stream editor.
It can perform a wide range of text processing functions.
Enjoy testing sedSED!特殊情况下的匹配替换存在/在字符串中时我们必须将/替换为#
r123localhost:~$ echo /abc/344555/abc | sed -e s#abc#ABC#g
/abcABC/344555/abcABC
r123localhost:~$ echo /abc/344555/abc | sed -e s#abc#ABC/g
sed: -e expression #1, char 12: unterminated s command
r123localhost:~$ echo /abc/344555/abc | sed -e s/abc/ABC/g
/abcABC/344555/abcABCsed从其他文件中添加内容
基本命令格式如下不添加匹配条件时默认添加到每一行后
sed -e [/匹配条件/]r 其他文件名 当前文件名在每一个符合匹配条件的行后增加文件内容
r123localhost:~/shell_code$ echo 12345678 1.txt
r123localhost:~/shell_code$ sed -e /sed/r 1.txt content.txt
Hello, World!
This is a test file for sed.
12345678
It contains some sample text that can be used to test various sed commands.
12345678
Feel free to modify this file and experiment with different sed commands.
12345678
sed is a powerful stream editor.
12345678
It can perform a wide range of text processing functions.
Enjoy testing sed!
12345678在当前文件后追加新文件内容
r123localhost:~/shell_code$ sed -r $r 1.txt content.txt
Hello, World!
This is a test file for sed.
It contains some sample text that can be used to test various sed commands.
Feel free to modify this file and experiment with different sed commands.
sed is a powerful stream editor.
It can perform a wide range of text processing functions.
Enjoy testing sed!
12345678sed文件内容另存为操作
基本命令格式如下不添加匹配条件时默认另存整个文件
sed -e [/匹配条件/]w 其他文件名 当前文件名r123localhost:~/shell_code$ sed -e /commands/w tmp.txt content.txt
Hello, World!
This is a test file for sed.
It contains some sample text that can be used to test various sed commands.
Feel free to modify this file and experiment with different sed commands.
sed is a powerful stream editor.
It can perform a wide range of text processing functions.
Enjoy testing sed!
r123localhost:~/shell_code$ cat tmp.txt
It contains some sample text that can be used to test various sed commands.
Feel free to modify this file and experiment with different sed commands.另存指定行的内容
r123localhost:~/shell_code$ sed -e 1,3w tmp.txt content.txt
Hello, World!
This is a test file for sed.
It contains some sample text that can be used to test various sed commands.
Feel free to modify this file and experiment with different sed commands.
sed is a powerful stream editor.
It can perform a wide range of text processing functions.
Enjoy testing sed!
r123localhost:~/shell_code$ cat tmp.txt
Hello, World!
This is a test file for sed.
It contains some sample text that can be used to test various sed commands.sed追加内容到指定位置
基本命令格式如下不添加匹配条件时默认追加到每行之后
sed -e [/匹配条件/]a追加内容 文件名r123localhost:~/shell_code$ sed -e /commands/a123456 content.txt
Hello, World!
This is a test file for sed.
It contains some sample text that can be used to test various sed commands.
123456
Feel free to modify this file and experiment with different sed commands.
123456
sed is a powerful stream editor.
It can perform a wide range of text processing functions.
Enjoy testing sed!追加内容到指定范围的行后
r123localhost:~/shell_code$ sed -e 3,5a123456 content.txt
Hello, World!
This is a test file for sed.
It contains some sample text that can be used to test various sed commands.
123456
Feel free to modify this file and experiment with different sed commands.
123456
sed is a powerful stream editor.
123456
It can perform a wide range of text processing functions.
Enjoy testing sed!在一行后面追加多行内容
r123localhost:~/shell_code$ sed -e 1a 233\23333\233333 content.txt
Hello, World!
233
23333
233333
This is a test file for sed.
...命令i与a的操作几乎是一致的不过i的作用是在指定行的前面添加内容这与a大同小异故不多赘述 r123localhost:~/shell_code$ sed -e 3i123456 content.txt
Hello, World!
This is a test file for sed.
123456
It contains some sample text that can be used to test various sed commands.
Feel free to modify this file and experiment with different sed commands.
sed is a powerful stream editor.
It can perform a wide range of text processing functions.
Enjoy testing sed!sed进行整行替换操作
基本命令格式如下不添加匹配条件时默认替换每一行
sed -e [/匹配条件/]c追加内容 文件名替换指定行号的内容
r123localhost:~/shell_code$ sed -e 2c 233333333 content.txt
Hello, World!
233333333
It contains some sample text that can be used to test various sed commands.
Feel free to modify this file and experiment with different sed commands.
sed is a powerful stream editor.
It can perform a wide range of text processing functions.
Enjoy testing sed!替换指定范围行号的内容为一行
r123localhost:~/shell_code$ sed -e 1,2c233333333 content.txt
233333333
It contains some sample text that can be used to test various sed commands.
Feel free to modify this file and experiment with different sed commands.
sed is a powerful stream editor.
It can perform a wide range of text processing functions.
Enjoy testing sed!sed操作目标行的下一行
我们使用{}来包含多个命令来执行这一操作n用于指定下一行
sed 选项 /匹配条件/{n;其他命令} 文件名r123localhost:~/shell_code$ sed -r /commands/{n; d} content.txt
Hello, World!
This is a test file for sed.
It contains some sample text that can be used to test various sed commands.
sed is a powerful stream editor.
It can perform a wide range of text processing functions.
Enjoy testing sed!sed命令反向操作文件
我们使用!来让命令作用范围反向操作
r123localhost:~/shell_code$ sed -e /commands/!d content.txt
It contains some sample text that can be used to test various sed commands.
Feel free to modify this file and experiment with different sed commands.r123localhost:~/shell_code$ sed -e /commands/d content.txt
Hello, World!
This is a test file for sed.
sed is a powerful stream editor.
It can perform a wide range of text processing functions.
Enjoy testing sed!sed执行多个操作的方式
使用多个e选项执行
r123localhost:~/shell_code$ sed -e 1d -e 3d content.txt
This is a test file for sed.
Feel free to modify this file and experiment with different sed commands.
sed is a powerful stream editor.
It can perform a wide range of text processing functions.
Enjoy testing sed!使用分号分割命令
r123localhost:~/shell_code$ sed -e 2d;3d content.txt
Hello, World!
Feel free to modify this file and experiment with different sed commands.
sed is a powerful stream editor.
It can perform a wide range of text processing functions.
Enjoy testing sed!使用{}执行多个命令
r123localhost:~/shell_code$ sed -e 2,4{s/sed/SED/g; s/commands/COMMANDS/g} content.txt
Hello, World!
This is a test file for sedSED.
It contains some sample text that can be usedSED to test various sedSED COMMANDS.
Feel free to modify this file and experiment with different sedSED COMMANDS.
sed is a powerful stream editor.
It can perform a wide range of text processing functions.
Enjoy testing sed!流编辑处理器awk
awk是一种强大的文本处理工具通常用于处理和分析文本文件中的数据。它以行为单位处理文本文件可以根据指定的规则对文件进行处理和转换。awk的基本工作流程是读取文件的每一行将每一行拆分成字段默认以空格分隔然后应用用户定义的操作对字段进行处理。
awk的基本结构如下
awk pattern { action } filename其中pattern是一个条件用于指定何时执行actionaction是要执行的操作。如果没有提供filenameawk将从标准输入读取数据。
例如要打印一个文件的第一列可以使用以下命令
awk { print $1 } filename这将打印文件中每一行的第一个字段以空格分隔。 注意awk实质上是一种完整的编程语言具有变量、数组、控制结构等基本编程元素。除了作为命令行工具使用外awk也可以编写独立的awk脚本文件其中包含更复杂的逻辑和功能。awk提供了丰富的内置函数和特殊变量使其在文本处理和数据转换方面非常强大和灵活。 awk的工作流程
awk的工作流程通常如下
读取文件awk从指定的文件或标准输入中逐行读取数据。分割行每行数据根据指定的分隔符默认为空格被拆分成多个字段。匹配模式对每一行数据awk根据用户提供的模式进行匹配。模式可以是简单的条件也可以是正则表达式。执行动作如果模式匹配成功awk执行与模式关联的动作。动作可以是打印、计算、赋值等操作。重复处理awk重复以上步骤直到处理完所有输入数据。输出结果根据执行的动作awk将生成的结果输出到标准输出或指定的文件中。
在分割行时awk实质上是先将当前读取到的行的数据储存在$0然后根据分割符将内容分组依次存储到$1$2…
awk的命令结构
awk 选项 命令 文件名选项功能-F指定字段分隔符-f指定awk脚本文件-v定义awk变量-i修改awk的工作模式-W控制awk的警告消息-o控制awk的输出格式-O控制awk的输出字段-F控制awk的输入字段
awk的特殊模式
awk中的BEGIN和END是特殊模式用于在处理输入之前和之后执行一次性操作。
使用BEGIN在处理之前打印标题
awk BEGIN { print Name\tAge\tGender } { print $1 \t $2 \t $3 } data.txt这个命令在处理data.txt之前会打印一行标题然后对每一行数据打印第一、第二、第三个字段。
使用END在处理之后打印汇总信息
awk { total $1 } END { print Total: total } data.txt注意BEGIN和END块中的代码只会执行一次分别在处理开始和结束时执行。
awk简单使用案例
r123localhost:~/shell_code$ sudo awk -F : BEGIN{ print start}{print $1 $2}END{print end} /etc/passwdstart
root x
daemon x
bin x
...
end实质上使用的时候其他操作比较少见sed就可以了这里唯一需要注意的是我们需要将其他的字符串以包括