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.txt 1到10行替换root 为oooo; /g 表示全局
sed 's/[0-9]//g' test.txt 数字全部删掉
sed 's/[^0-9]//g' test.txt 非数字全部删掉
sed -r 's/([^:]+)(:.*:)([^:]+$)/321/' 交换位置
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