位置元字符 | 含义 |
---|---|
^ | 匹配行尾 ,^a就是以a开头,默认锚定一个字符 |
$ | 匹配行首 ,a$就是以a结尾,默认锚定一个字符 |
aaasdc
a_c
abbbc
abbc
bcc
dddc
dc
aa
bbcc
aZc
abc
ac
da
ddb
asdfda
# 精确匹配
[marc@localhost Shell]$ egrep "^ac$" regular_expression_file
ac
# 模糊匹配
[marc@localhost Shell]$ egrep "^a" regular_expression_file
aaasdc
a_c
abbbc
abbc
aa
aZc
abc
ac
asdfda
[marc@localhost Shell]$ egrep "c$" regular_expression_file
aaasdc
a_c
abbbc
abbc
bcc
dddc
dc
bbcc
aZc
abc
ac
特殊字符 | 含义 |
---|---|
. | 匹配 除回车以外的任意一个字符 |
( ) | 字符串分组 |
[ ] | 定义字符类,匹配中括号中的任意一个字符 |
[ ^ ] | 取反,表示出括号中出现字符以外的字符 |
\ | 转义字符 |
| | 管道 |
[marc@localhost Shell]$ egrep "^a.c$" regular_expression_file
a_c
aZc
abc
[marc@localhost Shell]$ egrep "^(a|d)c$" regular_expression_file
dc
ac
数量元字符 | 含义 |
---|---|
* | 匹配前一个字符0~任意次 |
+ | 匹配前一个字符1~任意次 |
? | 匹配前一个字符0~1次 |
{n,m} | 匹配前一个字符n ~ m次 |
{n} | 匹配前一个字符 n 次 |
[marc@localhost Shell]$ egrep "^ab*c$" regular_expression_file
abbbc
abbc
abc
ac
[marc@localhost Shell]$ egrep "^ab?c$" regular_expression_file
abc
ac
[marc@localhost Shell]$ egrep "^ab+c$" regular_expression_file
abbbc
abbc
abc
POSIX字符 | 含义 |
---|---|
[:alnum:] | 匹配 任意数字字母字符 0-9 a-z A-Z |
[:alpha:] | 匹配 任意字母 ,大写或小写 a-z A-Z |
[:digit:] | 匹配 任意数字 0-9 |
[:graph:] | 匹配 非空字符 ,非空格控制字符 |
[:lower:] | 匹配 小写字母 a-z |
[:upper:] | 匹配 大写字母 A-Z |
[:ctrl:] | 匹配 控制字符 |
[:print:] | 匹配非空字符 ,包括空格 |
[:punct:] | 匹配标点符号 |
[:blank:] | 匹配空格和TAB字符 |
[:xdigit:] | 匹配16进制数字 |
[:space:] | 匹配所有空白字符,新行、空格、制表符 |
[marc@localhost Shell]$ egrep "^a[[:alnum:]]c$" regular_expression_file
aZc
abc
[marc@localhost Shell]$ egrep "^a[[:upper:]]c$" regular_expression_file
aZc
[marc@localhost Shell]$ egrep "^a[[:lower:]]c$" regular_expression_file
abc
[marc@localhost Shell]$ egrep "^a[[:alnum:]]*c$" regular_expression_file
aaasdc
abbbc
abbc
aZc
abc
ac
sed命令是Linux提供的一个外部命令,直接对文本进行操作,非交互式的对文件进行增、删、改、查等操作。使用时,只需在命令行输入编辑命令、指定文件名,然后在屏幕上查看输出。它和文本编辑器有本质区别:
sed从文本文件中读入一行到内存中,然后在内存中对文件进行修改,修改完成后打印到屏幕。
语法:sed [options] ‘{command}[flags]’ [filename]
命令选项:
-e script 将脚本中指定的命令添加到处理输入时执行的命令中,多条件, 一行中要有多个条件 如: sed -e 's/brown/green/;s/dog/cat/' sed_file # 多条命令
-f filename 将处理文件时输入的执行命令添加到指定的文件中 如: sed -f command_file sed_file (command_file文件中包含了要执行的命令)
-n 抑制自动输出 如: sed -n '2,3s/dog/cat/p' sed_file # 只打印出有用内容
-i 编辑文件内容 如: sed -i 's/dog/cat/g' sed_file # 源文件被修改(不可逆)
-i.bak 修改时同时创建.bak备份文件 如: sed -i.bak 's/cat/dog/g' sed_file # 修改源文件的同时创建备份文件
-r 使用扩展的正则表达式 如: sed -n -r '/^(root)(.*)(bash)$/p' /etc/passwd
! 取反(跟在模式条件后与shell有所区别)
sed常用内部命令:
a 在匹配后面添加 如: sed '/3 the/a\hello world' sed_file # /3 the/ 表示匹配含有3 the的行,a表示行后面添加,hello world为要添加的内容
i 在匹配前面添加 如: sed '/3 the/i\hello world' sed_file
p 打印 如: sed '/3 the/p' sed_file # 第3行打印2遍,其余行打印一遍
d 删除 如: sed '/3 the/d' sed_file
s 直接替换 如: sed '2,4s/dog/cat/' sed_file # 将2-4行中的出现的第一个dog替换为cat ,可以添加g进行全局替换(行)
c 更改 如: sed '2,4c\hello world' sed_file # 直接将2-4行的所有内容替换为一行指定添加的内容
y 转换 N D P 如: sed 'y/acbdef/ABCXYZ/' sed_file
flags:对命令进行补充
数字 表示新文本替换的模式 如: sed 's/dog/cat/2' sed_file # 将每一行第2次出现的dog替换为cat
g 表示新文本替换现有文本的全部实例 如: sed '2,4s/dog/cat/g' sed_file # 全局替换, 将第2-4行出现的所有的dog全部替换为cat
p 表示打印原始的内容 如: sed '3s/dog/cat/p' sed_file # 替换的同时进行打印, 可以同时使用s///gp
w filename 将替换的结果写入文件 如: sed '2,4s/dog/cat/w m_file' sed_file # 将替换后的结果写入m_file文件中,系统会自动创建文件
演示文件1:
1 the quick brown fox jumps over the lazy dog
2 the quick brown fox jumps over the lazy dog
3 the quick brown fox jumps over the lazy dog
4 the quick brown fox jumps over the lazy dog
5 the quick brown fox jumps over the lazy dog
########################### sed内部常用命令 ###################################
1) a 在匹配后面添加 —— 可以使用转义字符\将a与添加内容分隔开
[marc@localhost Shell]$ sed 'a\hello world' sed_file # 在每一行后面添加上hello world
1 the quick brown fox jumps over the lazy dog
hello world
2 the quick brown fox jumps over the lazy dog
hello world
3 the quick brown fox jumps over the lazy dog
hello world
4 the quick brown fox jumps over the lazy dog
hello world
5 the quick brown fox jumps over the lazy dog
hello world
[marc@localhost Shell]$ sed '3a\hello world' sed_file # 在第3行后面添加上hello world
1 the quick brown fox jumps over the lazy dog
2 the quick brown fox jumps over the lazy dog
3 the quick brown fox jumps over the lazy dog
hello world
4 the quick brown fox jumps over the lazy dog
5 the quick brown fox jumps over the lazy dog
[marc@localhost Shell]$ sed '2,4a\hello world' sed_file # 在第2-4行后面添加上hello world
1 the quick brown fox jumps over the lazy dog
2 the quick brown fox jumps over the lazy dog
hello world
3 the quick brown fox jumps over the lazy dog
hello world
4 the quick brown fox jumps over the lazy dog
hello world
5 the quick brown fox jumps over the lazy dog
[marc@localhost Shell]$ sed '/3 the/a\hello world' sed_file # 通过/ / 匹配,在第3行后面添加上hello world
1 the quick brown fox jumps over the lazy dog
2 the quick brown fox jumps over the lazy dog
3 the quick brown fox jumps over the lazy dog
hello world
4 the quick brown fox jumps over the lazy dog
5 the quick brown fox jumps over the lazy dog
2) i 在匹配前面添加 —— sed命令只改变输出在屏幕上的内容,原文本自身的并不发生改变
[marc@localhost Shell]$ sed 'i\hello world' sed_file
[marc@localhost Shell]$ sed '2i\hello world' sed_file
[marc@localhost Shell]$ sed '2,4i\hello world' sed_file
[marc@localhost Shell]$ sed '/3 the/i\hello world' sed_file
3) p 打印 —— 输出两遍内容, 打印一遍文件内容,同时又从缓存输出一遍
[marc@localhost Shell]$ sed 'p' sed_file
1 the quick brown fox jumps over the lazy dog
1 the quick brown fox jumps over the lazy dog
2 the quick brown fox jumps over the lazy dog
2 the quick brown fox jumps over the lazy dog
3 the quick brown fox jumps over the lazy dog
3 the quick brown fox jumps over the lazy dog
4 the quick brown fox jumps over the lazy dog
4 the quick brown fox jumps over the lazy dog
5 the quick brown fox jumps over the lazy dog
5 the quick brown fox jumps over the lazy dog
[marc@localhost Shell]$ sed '3p' sed_file
[marc@localhost Shell]$ sed '2,4p' sed_file # 第2-4行打印2遍,其余行打印一遍
[marc@localhost Shell]$ sed '/3 the/p' sed_file # 第3行打印2遍,其余行打印一遍
4) d 删除 —— 常使用匹配删除
[marc@localhost Shell]$ sed 'd' sed_file
[marc@localhost Shell]$ sed '3d' sed_file
[marc@localhost Shell]$ sed '2,4d' sed_file
[marc@localhost Shell]$ sed '/3 the/d' sed_file # 删除第3行
5) s 直接替换 —— 使用方法 s/ / /
[marc@localhost Shell]$ sed 's/dog/cat/' sed_file # 替换所有行中的dog
[marc@localhost Shell]$ sed '3s/dog/cat/' sed_file # 替换第3行中的dog
[marc@localhost Shell]$ sed '2,4s/dog/cat/' sed_file
[marc@localhost Shell]$ sed '/3 the/s/dog/cat/' sed_file # 先匹配第3行,在替换第3行中的dog
6) c 更改 —— 指定多行更改时,会直接将其看做一行进行更改
[marc@localhost Shell]$ sed 'c\hello world' sed_file
hello world
hello world
hello world
hello world
hello world
[marc@localhost Shell]$ sed '3c\hello world' sed_file
1 the quick brown fox jumps over the lazy dog
2 the quick brown fox jumps over the lazy dog
hello world
4 the quick brown fox jumps over the lazy dog
5 the quick brown fox jumps over the lazy dog
[marc@localhost Shell]$ sed '2,4c\hello world' sed_file # 直接将2-4行的所有内容替换为一行指定添加的内容
1 the quick brown fox jumps over the lazy dog
hello world
5 the quick brown fox jumps over the lazy dog
[marc@localhost Shell]$ sed '/3 the/c\hello world' sed_file # 先匹配,再更改
1 the quick brown fox jumps over the lazy dog
2 the quick brown fox jumps over the lazy dog
hello world
4 the quick brown fox jumps over the lazy dog
5 the quick brown fox jumps over the lazy dog
7) y 转换 —— 一一对应,且不能使用类似范围操作符的符号
[marc@localhost Shell]$ sed 'y/acbdef/ABCXYZ/' sed_file
1 thY quiBk Crown Zox jumps ovYr thY lAzy Xog
2 thY quiBk Crown Zox jumps ovYr thY lAzy Xog
3 thY quiBk Crown Zox jumps ovYr thY lAzy Xog
4 thY quiBk Crown Zox jumps ovYr thY lAzy Xog
5 thY quiBk Crown Zox jumps ovYr thY lAzy Xog
演示文件2:
1 the quick brown fox jumps over the lazy dog,dog
2 the quick brown fox jumps over the lazy dog,dog
3 the quick brown fox jumps over the lazy dog,dog
4 the quick brown fox jumps over the lazy dog,dog
5 the quick brown fox jumps over the lazy dog,dog
############################### flags标志位用法 ##############################
1)[marc@localhost Shell]$ sed 's/dog/cat/2' sed_file # 数字
1 the quick brown fox jumps over the lazy dog,cat
2 the quick brown fox jumps over the lazy dog,cat
3 the quick brown fox jumps over the lazy dog,cat
4 the quick brown fox jumps over the lazy dog,cat
5 the quick brown fox jumps over the lazy dog,cat
2)[marc@localhost Shell]$ sed '3s/dog/cat/p' sed_file # p 打印
1 the quick brown fox jumps over the lazy dog,dog
2 the quick brown fox jumps over the lazy dog,dog
3 the quick brown fox jumps over the lazy cat,dog
3 the quick brown fox jumps over the lazy cat,dog
4 the quick brown fox jumps over the lazy dog,dog
5 the quick brown fox jumps over the lazy dog,dog
[marc@localhost Shell]$ sed 's/dog/cat/g' sed_file # g 全局替换
1 the quick brown fox jumps over the lazy cat,cat
2 the quick brown fox jumps over the lazy cat,cat
3 the quick brown fox jumps over the lazy cat,cat
4 the quick brown fox jumps over the lazy cat,cat
5 the quick brown fox jumps over the lazy cat,cat
[marc@localhost Shell]$ sed '2,4s/dog/cat/w m_file' sed_file # w filename 写入文件
1 the quick brown fox jumps over the lazy dog,dog
2 the quick brown fox jumps over the lazy cat,dog
3 the quick brown fox jumps over the lazy cat,dog
4 the quick brown fox jumps over the lazy cat,dog
5 the quick brown fox jumps over the lazy dog,dog
[marc@localhost Shell]$ cat m_file
2 the quick brown fox jumps over the lazy cat,dog
3 the quick brown fox jumps over the lazy cat,dog
4 the quick brown fox jumps over the lazy cat,dog
########################### 命令选项 #############################333
1) -n 抑制自动输出 —— 用于打印时避免不必要的输出
[marc@localhost Shell]$ sed -n '2,3s/dog/cat/p' sed_file # 只打印出有用内容
2 the quick brown fox jumps over the lazy cat,dog
3 the quick brown fox jumps over the lazy cat,dog
2) -e script 将脚本中指定的命令添加到处理输入时执行的命令中,多条件, 一行中要有多个条件
[marc@localhost Shell]$ sed -e 's/brown/green/;s/dog/cat/' sed_file # 多条命令
1 the quick green fox jumps over the lazy cat,dog
2 the quick green fox jumps over the lazy cat,dog
3 the quick green fox jumps over the lazy cat,dog
4 the quick green fox jumps over the lazy cat,dog
5 the quick green fox jumps over the lazy cat,dog
3)-f file_name 将处理文件时输入的执行命令添加到指定的文件中
[marc@localhost Shell]$ g command_file # 文件command_file中的内容即为:s/brown/green/;s/dog/cat/ 可改写成两行
[marc@localhost Shell]$ sed -f command_file sed_file
1 the quick green fox jumps over the lazy cat,dog
2 the quick green fox jumps over the lazy cat,dog
3 the quick green fox jumps over the lazy cat,dog
4 the quick green fox jumps over the lazy cat,dog
5 the quick green fox jumps over the lazy cat,dog
4) -i 修改源文件
[marc@localhost Shell]$ sed -i 's/dog/cat/g' sed_file # 源文件被修改(不可逆)
[marc@localhost Shell]$ cat sed_file
1 the quick brown fox jumps over the lazy cat,cat
2 the quick brown fox jumps over the lazy cat,cat
3 the quick brown fox jumps over the lazy cat,cat
4 the quick brown fox jumps over the lazy cat,cat
5 the quick brown fox jumps over the lazy cat,cat
5) -i.bak 修改源文件同时创建.bak备份文件
[marc@localhost Shell]$ sed -i.bak 's/cat/dog/g' sed_file # 修改源文件的同时创建备份文件
[marc@localhost Shell]$ ls
sed_file sed_file.bak
[marc@localhost Shell]$ cat sed_file.bak
1 the quick brown fox jumps over the lazy cat,cat
2 the quick brown fox jumps over the lazy cat,cat
3 the quick brown fox jumps over the lazy cat,cat
4 the quick brown fox jumps over the lazy cat,cat
5 the quick brown fox jumps over the lazy cat,cat
[marc@localhost Shell]$ cat sed_file
1 the quick brown fox jumps over the lazy dog,dog
2 the quick brown fox jumps over the lazy dog,dog
3 the quick brown fox jumps over the lazy dog,dog
4 the quick brown fox jumps over the lazy dog,dog
5 the quick brown fox jumps over the lazy dog,dog
6)-r 使用扩展的正则表达式 —— 元字符为标志
[marc@localhost Shell]$ sed -n -r '/^(root)(.*)(bash)$/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[marc@localhost Shell]$ echo "marc is cool" | sed 's/cool/wonderful/'
marc is wonderful
[marc@localhost Shell]$ sed -n '$=' sed_file
5
awk命令是Linux提供的一个外部命令,不同于sed,awk命令可以实现数据的处理、运算、输出,功能十分强大。awk认为文件中的每一行是一条记录,记录与记录之间的分隔符为换行符;每一列是一个字段,字段与字段之间的分隔符默认是一个或多个空格或Tab制表符。
可实现的基本功能有:
详情见:链接: link.