grep, sed 和 awk 学习总结

以前粗略学习过,今天再复习一遍并做笔记,方便自己查阅。

1. grep/egrep:

grep 'root' test.txt显示出 root 关键字所在行
grep -c 'root' test.txt显示存在关键字 root 的行的总数
grep -n 'root' test.txt显示出 root 关键字所在行以及行号,如: 1:root:x:0:0:root:/root:/bin/bash
grep -n --color 'root' test.txt显示出 root 关键字所在行以及行并且行号颜色标记,如:1:root:x:0:0:root:/root:/bin/bash

alias grep='grep --color'

grep -n 'root' test.txt

同上。 alias 是设置别名,删除别名用:unalias grep
grep -o 'root' test.txt|wc -l显示出该文本中 root 关键字的总个数
grep -o 'root' test.txt显示出该文本中所有的 root 关键字
grep -v 'root' test.txt取反: 显示出所有不包含 root 关键字的行
grep -A1 -n 'root' test.txt显示包含关键字 root 的行以及后面1行,也可写A2,A3.....
grep -B1 -n 'root' test.txt显示包含关键字 root 的行以及前面1行,也可写B2,B3.....
grep -C1 -n 'root' test.txt显示包含关键字 root 的行以及前后各1行,也可写C1,C2.....
grep -r 'root' /etc/遍历文件夹下所有文件中有关键字 root 的行
grep '[0-9]' test.txt过滤显示包含数字的行
grep -v '[0-9]' test.txt过滤显示不包含数字的行
grep -v '^#' test.txt显示不以#开头的行
grep -n 'n$' test.txt显示以n 结尾的行
grep -v '^$' test.txt|grep -v '^#' 显示除空行和以“#”开头的行以外的行
grep '^[a-zA-Z]' test.txt显示以字母开头的行
grep -v '^[0-9]' test.txt 或者 grep '^[^0-9]' test.txt显示非数字开头的行
grep 'ro*t' test.txt* 代表0/多个前面的字符,即rt,rot,rooooooooo....t
grep -E 'ro+t' test.txt 或者egrep 'ro+t' test.txt+ 代表1/多个前面的字符,即rot,rooooooooo....t
grep 'ro.t' test.txt. 代表任意一个字符,即roat,robt,ro0t,....
grep -E 'ro?t' test.txt 或者 egrep 'ro?t' test.txt? 代表0/1个前面的字符,即rt,rot
grep 'ro.*t' test.txt显示有 ro 开头 t 结尾的关键字所在的行

egrep 'root|mysql' test.txt 或者 grep 'root\|mysql' test.txt

或者 grep -E 'root|mysql' test.txt

显示有root 或者mysql 关键字的行
grep -E '(oo)+' test.txt显示有1个或多个oo 的行
grep -E '(oo){2}' test.txt显示2个oo 的行

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2. sed

sed '1p' -n test.txt打印第1行
grep -n '.*' test.txt|sed '1p' -n打印第一行并且显示行号
sed '1,5p' -n test.txt打印前5行
sed '5,$p' -n test.txt打印第5行到最后
sed '/root/p' -n test.txt打印包含关键字 root 的行
sed -r '/ro?t/p' -n test.txt 或者sed '/ro\?t/p' -n test.txt+,? 需要加-r 或者转义符号\
sed -e '/root/p' -e '/mysql/p' -n test.txt打印包含 root 或者mysql 关键字的行
sed '/root/p;/mysql/p' -n test.txt打印包含 root 或者mysql 关键字的行
sed '1,5d' test.txt删除1到第5行
sed -r '/root|mysql/d' test.txt删除有root或mysql的行
sed '1,10s/root/oooo/g' test.txt1到10行替换root 为oooo; /g 表示全局
sed 's/[0-9]//g' test.txt数字全部删掉
sed 's/[^0-9]//g' test.txt非数字全部删掉
sed -r 's/([^:]+)(:.*:)([^:]+$)/\3\2\1/'交换位置
sed -i 's/root/oooo/g' test.txt更改源文件所有的root 替换为oooo, 只有加了-i 才会更改源文件

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

sed 有匹配的功能,但是它的主要功能还是替换,如果单纯过滤匹配用grep更方便,不加 -i 的情况下只是显示,只有加了 -i 才会真正更改文件

 

 

3. awk

awk -F 'root' '{print $2}' test.txt以root为分隔符,打印第二段,不加-F 默认以空格隔开
awk -F ':' '$1~/root/' test.txt打印以 : 隔开,第一段包含关键字 root 的 行
awk -F ':' '$1~/root/ {print $3,$4}' test.txt打印以 : 隔开,第一段包含关键字 root 的行的第3和第4段
awk -F ':' '{OFS="...";} $1~/root/ {print $3,$4}'打印以 : 隔开,第一段包含关键字 root 的行的第3和第4段,并且显示结果以“...” 隔开
awk -F ':' '$1=="root" {print $1,$2}' test.txt打印以 : 隔开,第一段等于root的第1和第2段
awk -F ':' '$1=="root" || NR >20 {print $1,$2}' test.txt打印以 : 隔开,第一段等于root 或者行数大于20 的第1和第2段
awk -F ':' '$1=="root" && NR >20 {print $1,$2}' test.txt打印以 : 隔开,第一段等于root 并且行数大于20 的第1和第2段
awk -F ':' 'NF>3 && NR<6 {print}' test.txt打印段数大于3行数小于6的行

 

转载于:https://www.cnblogs.com/nothingc/p/7159778.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值