Shell—三剑客(grep、sed、awk)

grep命令详解

文本搜索工具,根据用户指定的“模式(pattern)”对目标文本进行过滤,显示被模式匹配到的行。

命令格式:grep  [options]  pattern  filename。grep适合单纯的查找或匹配文本。grep是区分大小写的。

匹配参数[options]

  • -i     不区分大小写,忽略字符大小写
  • -v    后面接啥排除啥,取反,显示不被pattern匹配到的行
  • -n    显示匹配结果的行号
  • -c    统计匹配结果的行数
  • -o    仅显示匹配到的字符串,不把整行显示出来
  • -e    实现多个选项的匹配,逻辑or关系
  • -q    静默模式,不输出任何信息。与"echo $"合用,查看是否匹配到,0表示匹配到,1表示没有匹配到
  • -Ax:显示匹配结果所在行以及该行之后的指定行数,x是行数,A:after。
  • -Bx:显示匹配结果所在行以及该行之前的指定行数,x是行数,B:before。
  • -Cx:显示匹配结果所在行以及该行之前和该行之后的指定行数,x是行数,C:context
  • --color             显示颜色
  • -E 使用ERE,相当于egrep
[root@localhost ~]# grep "root" /etc/passwd      # 找到root所在的所有行并显示
[root@localhost ~]# grep -v "root" /etc/passwd   # 找到除root外的所有行并显示
[root@localhost ~]# grep -n "root" /etc/passwd   # 显示行号
[root@localhost ~]# grep -c "root" /etc/passwd   # 显示匹配结果的行数
[root@localhost ~]# grep -A2 "root" /etc/passwd  # 匹配含有root的行,以及该行的后两行
[root@localhost ~]# grep -e "root" -e "myuser" /etc/passwd

sed命令详解

sed 是一种在线的、非交互式的编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。
sed 主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。
sed 和grep不一样,不管是否找到指定的模式,它的退出状态都是0。只有当命令存在语法错误时,sed的退出状态才是非0。

实现数据的替换,删除,增加,选取等(以行为单位进行处理)

1.打印显示文本内容

[root@localhost ~]# sed -n '3p' test.sh        # 打印文件的第3行。
[root@localhost ~]# sed -n '$p' test.sh        # 打印文件的最后一行
[root@localhost ~]# sed -n '3,6p' test.sh      # 打印文件的第3行到第6行。
[root@localhost ~]# sed -n '3,$p' test.sh      # 打印文件的第3行到最后一行的内容。
[root@localhost ~]# sed -n '3~2p' test.sh      # 从第3行开始,每隔两行打印一行,波浪号后面的2表示步长。

[root@localhost ~]# sed -n '/love/p' test.sh    #  逐行读取文件,打印匹配love的行
[root@localhost ~]# sed -n '/love/,3p' test.sh  # 逐行读取文件,打印从匹配love的行到第3行的内容,也打印后面所有匹配love 的行。
[root@localhost ~]# sed -n '/love/,$p' test.sh  # 逐行读取文件,打印从匹配too的行到最后一行的内容。
[root@localhost ~]# sed -n '/love/,+1p'  test.sh    #打印匹配too的行及其向后一行,如果有多行匹配too,则匹配的每一行都会向后多打印一行
[root@localhost ~]# sed -n '/love/,/you/p'  1.txt   #打印从匹配内容love到匹配内容you的行
[root@localhost ~]# sed -n '3,/love/p' test.sh  # 打印第三行到匹配love的行

# 打印test.sh文件最后一行的行号(即文件有多少行,和wc -l 功能类似)
[root@localhost ~]# sed -n "$=" test.sh
# 打印匹配error的行的行号
[root@localhost ~]# sed -n '/error/='  test.sh    
# 打印匹配error的行的行号和内容(可用于查看日志中有error的行及其内容)
[root@localhost ~]# sed -n '/error/{=;p}'   test.sh

2.增加文件内容,向文件中添加或插入行

# 在第三行后面添加python,3表示行号
[root@localhost ~]# sed '3apython' test.sh
# 在第三行之前插入python,3表示行号
[root@localhost ~]# sed '3ipython' test.sh

# 在最后一行之后添加python
[root@localhost ~]# sed '$apython' test.sh
# 在最后一行之前插入python
[root@localhost ~]# sed '$ipython' test.sh

# 在包含123的行后面添加python,如果文件中有多行包括123,则每一行后面都会添加
[root@localhost ~]# sed '/123/apython' test.sh
# 在包含123的行之前插入python,如果文件中有多行包含123,则每一行之前都会插入
[root@localhost ~]# sed '/123/ipython'  test.sh  

3.删除文件中指定的行

[root@localhost ~]# sed '3d' 1.txt       # 删除第三行
[root@localhost ~]# sed '$d' 1.txt       # 删除最后一行
[root@localhost ~]# sed '1~2d' 1.txt     # 从第一行开始删除,每隔2行就删掉一行,即删除奇数行
[root@localhost ~]# sed '1,3d'  1.txt    # 删除1~3行
[root@localhost ~]# sed '1,3!d'  1.txt   # 删除1~3之外的所有行

[root@localhost ~]# sed '/123/d'  1.txt       # 删除匹配123的行
[root@localhost ~]# sed '/123/,$d' 1.txt      # 删除从匹配123的行到最后一行
[root@localhost ~]# sed '/123/,+1d' 1.txt     # 删除匹配123的行及其后面一行
[root@localhost ~]# sed '/^$/d'   1.txt       # 删除空行
[root@localhost ~]# sed '/123|abc/!d' 1.txt  # 删除不匹配123或abc的行,/123|abc/ 表示匹配123或abc ,!表示取反

4.更改文件中指定的行

[root@localhost ~]# sed '1chello' test.sh         # 将文件的第一行替换为hello   
[root@localhost ~]# sed '$chello' test.sh         # 将文件的最后一行替换为hello
[root@localhost ~]# sed '/123/chello' test.sh     # 将包含123的行替换为hello

https://www.runoob.com/linux/linux-comm-sed.htmlhttps://www.cnblogs.com/ftl1012/p/9250438.htmlhttps://blog.csdn.net/wdz306ling/article/details/80087889https://www.jianshu.com/p/d9f40945242bhttps://blog.csdn.net/a13822665196/article/details/102171573

awk命令详解

原文地址:https://www.cnblogs.com/liuhaidon/p/11564010.html