4.9 三剑客之sed(查找、替换、删除、插入)
sed参数 |
说明 |
-n |
明确指定时打印匹配的行或处理过的行,如果不加-n会自动打印经过处理的每一行 |
-r |
支持扩展正则 |
-i |
修改源文件 |
4.9.1 sed指定行输出 [行数]p
案例144. sed指定行输出,显示文件的第三行
| [root@kylin-ren-class ren] |
| 学的是运维 全名叫云计算运维 |
案例145. sed显示文件的第3到6行的内容
| [root@kylin-ren-class ren]# sed -n '3,6p' a.txt |
| 学的是运维 全名叫云计算运维 |
| |
| |
| 运维 |
案例146. sed显示文件的第3行到末尾的内容
| [root@kylin-ren-class ren]# sed -n '3,$p' c.txt |
| ren |
| renpengyu2234 |
| 运维 |
| 运维 |
4.9.2 sed模糊搜索 /[字符串]/p
| 类似于grep |
| grep '字符串' file |
| sed -n '/字符串/p' file |
案例147. 查找包含ren的行
| [root@kylin-ren-class ren]# sed -n '/ren/p' c.txt |
| renpengyu |
| renpengyu12323123 |
| ren |
| renpengyu2234 |
案例148. 查找以“学”开头的行
| [root@kylin-ren-class ren] |
| 学的是运维 全名叫云计算运维 |
案例149. 查找以“空格”结尾的行
| [root@kylin-ren-class ren] |
| 你好 任鹏宇 我在学习 |
| 学的是运维 全名叫云计算运维 |
案例150. 查找包含“运维“或者”学”的行
| [root@kylin-ren-class ren] |
| 你好 任鹏宇 我在学习 |
| 学的是运维 全名叫云计算运维 |
| 运维 |
案例151. 查找以“!.$”结尾的行
| [root@kylin-ren-class ren] |
| The monkeys were swinging from branch to branch and making funny faces. I laughed a lot! |
| not 572891999997. |
| ^^^^^^^^66$$$$$$$^^^$$ |
案例152. sed支持扩展正则
| [root@kylin-ren-class ren] |
| 你好 任鹏宇 我在学习 |
| 学的是运维 全名叫云计算运维 |
| 运维 |
4.9.3 按照区间进行过滤查找 [/内容/,/内容/p]
案例153. 查找“学”和“2”之间的内容
| [root@kylin-ren-class ren]# sed -n '/学/,/2/p' a.txt |
| 你好 任鹏宇 我在学习 |
| 学的是运维 全名叫云计算运维 |
| 运维 |
| 66 |
| 2352345 |
案例154. 过滤日志时间范围,09:25:01点到09:26:01点之间的内容
| [root@kylin-ren-class ren]# sed -n '/09:25:01/,/09:26:01/p' /var/log/messages |
| Jan 7 09:25:01 kylin-ren-class systemd[1]: Started Session 40 of user root. |
| Jan 7 09:25:01 kylin-ren-class systemd[1]: session-40.scope: Succeeded. |
| Jan 7 09:25:06 kylin-ren-class systemd[1]: kylin-kms-activation.service: Service RestartSec=30s expired, scheduling restart. |
| Jan 7 09:25:06 kylin-ren-class systemd[1]: kylin-kms-activation.service: Scheduled restart job, restart counter is at 25. |
| Jan 7 09:25:06 kylin-ren-class systemd[1]: Stopped run kylin_kms_daemon at boot time. |
| Jan 7 09:25:06 kylin-ren-class systemd[1]: Started run kylin_kms_daemon at boot time. |
| Jan 7 09:26:01 kylin-ren-class systemd[1]: Started Session 41 of user root. |
[!CAUTION]
注意:/[内容]1/,/[内容2]/p,如果没有匹配到“内容1”则不会匹配“内容2”,如果出现多对“内容1”与“内容2”那么都会进行匹配
4.9.4 删除内容 [内容]d
| 语法结构: |
| sed '3d' file 删除指定行 |
| sed '/adm/d' file 模糊过滤到的行删除 |
案例155. 删除第3行内容
| [root@kylin-ren-class ren] |
| 1 你好 任鹏宇 我在学习 |
| 2 学的是运维 全名叫云计算运维 |
| 3 运维 |
| 4 66 |
| 5 2352345 |
| 6 435345 |
| |
| [root@kylin-ren-class ren] |
| 你好 任鹏宇 我在学习 |
| 学的是运维 全名叫云计算运维 |
| 66 |
| 2352345 |
| 435345 |
案例156. 删除3到5行内容
| [root@kylin-ren-class ren]# sed '3,5d' a.txt |
| 你好 任鹏宇 我在学习 |
| 学的是运维 全名叫云计算运维 |
| 435345 |
案例157. 删除模糊搜索到的行
| [root@kylin-ren-class ren] |
| 你好 任鹏宇 我在学习 |
| 学的是运维 全名叫云计算运维 |
| 运维 |
| 2352345 |
| 435345 |
案例158. 删除包含“学”和“任鹏宇”的行
| [root@kylin-ren-class ren] |
| 运维 |
| 66 |
| 2352345 |
| 435345 |
案例159. 删除区间范围的行
| [root@kylin-ren-class ren]# sed '/6/,/4/d' a.txt |
| 你好 任鹏宇 我在学习 |
| 学的是运维 全名叫云计算运维 |
| 运维 |
| 435345 |
[!CAUTION]
注意://,//d 这个会匹配第一组“6”和“4”,如果有第二个“4”但是没有“6”则不会进行继续匹配
4.9.5 增加内容 i、a、c、w
| 语法格式: |
| sed '3i 字符串' file |
| sed '3a 字符串' file |
| sed '3c 字符串' file |
| sed '3w 字符串' file |
案例173. 在第3行插入“任鹏宇"字符串
| [root@kylin-ren-class ren] |
| root:x:0:0:root:/root:/bin/bash |
| bin:x:1:1:bin:/bin:/sbin/nologin |
| 任鹏宇 |
| daemon:x:2:2:daemon:/sbin:/sbin/nologin |
案例174. 在第3行下面追加插入“任鹏宇”字符串
| [root@kylin-ren-class ren] |
| root:x:0:0:root:/root:/bin/bash |
| bin:x:1:1:bin:/bin:/sbin/nologin |
| daemon:x:2:2:daemon:/sbin:/sbin/nologin |
| 任鹏宇 |
| adm:x:3:4:adm:/var/adm:/sbin/nologin |
案例175. 将第3行内容替换成“任鹏宇“
| [root@kylin-ren-class ren] |
| root:x:0:0:root:/root:/bin/bash |
| bin:x:1:1:bin:/bin:/sbin/nologin |
| 任鹏宇 |
| adm:x:3:4:adm:/var/adm:/sbin/nologin |
案例176. 指定第4行的内容保存在新文件
| [root@kylin-ren-class ren] |
| [root@kylin-ren-class ren] |
| daemon:x:2:2:daemon:/sbin:/sbin/nologin |
4.9.6 替换内容 s#将谁#替换成谁#g、s@将谁@替换成@g、s/将谁/替换成/g
| 语法结构: |
| sed 's#将谁#替换成#g' file |
| sed 's@将谁@替换成@g' file |
| sed 's/将谁/替换成/g' file |
| 准备内容: |
| [root@kylin-ren-class ren] |
| 1 root:x:0:0:root:/root:/bin/bash |
| 2 bin:x:1:1:bin:/bin:/sbin/nologin |
| 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin |
| 4 adm:x:3:4:adm:/var/adm:/sbin/nologin |
| 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin |
| 6 sync:x:5:0:sync:/sbin:/bin/sync |
| 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown |
| 8 halt:x:7:0:halt:/sbin:/sbin/halt |
| 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin |
| 10 operator:x:11:0:operator:/root:/sbin/nologin |
案例160. 将每行的第一个root替换成ren字符串
| [root@kylin-ren-class ren] |
| renpengyu:x:0:0:root:/root:/bin/bash |
[!CAUTION]
注意:s### 是替换第一个匹配到的内容,如果是s###g则是替换全文
案例161. 将所有的root替换成renoengyu
| [root@kylin-ren-class ren] |
| renpengyu:x:0:0:renpengyu:/renpengyu:/bin/bash |
案例162. 将a-z替换成空
| [root@kylin-ren-class ren] |
| : :0:0: :/ :/ / |
| : :1:1: :/ :/ / |
| : :2:2: :/ :/ / |
| : :3:4: :/ / :/ / |
| : :4:7: :/ / / :/ / |
| : :5:0: :/ :/ / |
| : :6:0: :/ :/ / |
| : :7:0: :/ :/ / |
| : :8:12: :/ / / :/ / |
| : :11:0: :/ :/ / |
案例163. 将a-z取反,除了a-z的内容替换成空
| [root@kylin-ren-class ren] |
| root x root root bin bash |
| bin x bin bin sbin nologin |
| daemon x daemon sbin sbin nologin |
| adm x adm var adm sbin nologin |
| lp x lp var spool lpd sbin nologin |
| sync x sync sbin bin sync |
| shutdown x shutdown sbin sbin shutdown |
| halt x halt sbin sbin halt |
| mail x mail var spool mail sbin nologin |
| operator x operator root sbin nologin |
案例164. 根据163案例后的结果,统计单词出现的次数
| [root@kylin-ren-class ren] |
| 12 sbin |
| 10 x |
| 6 nologin |
| 5 bin |
| 4 root |
| 3 var |
| 3 sync |
| 3 shutdown |
| 3 mail |
| 3 halt |
| 3 adm |
| 2 spool |
| 2 operator |
| 2 lp |
| 2 daemon |
| 1 lpd |
| 1 bash |
| |
案例165. 替换文档中的 “😕” 为空格
| [root@kylin-ren-class ren] |
| root:x:0:0:root root bin/bash |
| bin:x:1:1:bin bin sbin/nologin |
| daemon:x:2:2:daemon sbin sbin/nologin |
| adm:x:3:4:adm var/adm sbin/nologin |
| lp:x:4:7:lp var/spool/lpd sbin/nologin |
| sync:x:5:0:sync sbin bin/sync |
| shutdown:x:6:0:shutdown sbin sbin/shutdown |
| halt:x:7:0:halt sbin sbin/halt |
| mail:x:8:12:mail var/spool/mail sbin/nologin |
| operator:x:11:0:operator root sbin/nologin |
案例166. 替换文档中的 ” : " 或者 " / "为空格
| [root@kylin-ren-class ren] |
| root x 0 0 root root bin bash |
| bin x 1 1 bin bin sbin nologin |
| daemon x 2 2 daemon sbin sbin nologin |
| adm x 3 4 adm var adm sbin nologin |
| lp x 4 7 lp var spool lpd sbin nologin |
| sync x 5 0 sync sbin bin sync |
| shutdown x 6 0 shutdown sbin sbin shutdown |
| halt x 7 0 halt sbin sbin halt |
| mail x 8 12 mail var spool mail sbin nologin |
| operator x 11 0 operator root sbin nologin |
案例167. 统计文件中除了a-z以外字符串的总长度
| [root@kylin-ren-class ren]# sed 's#[a-z]##g' user.log | while IFS= read -r line;do echo -n "$line" | wc -c; done |
| 11 |
| 11 |
| 11 |
| 12 |
| 13 |
| 11 |
| 11 |
| 11 |
| 14 |
| 12 |
[!NOTE]
| 解析: |
| 1.首先使用sed命令将a-z的字符替换为空 |
| 2.使用while循环,其中IFS = read -r line,将传递过来的内容进行逐行处理 |
| 3.使用echo -n,其中-n参数可以排除换行符,默认的话每一行内容其实有一个换行符。echo -n "$line"可以逐行输出 |
| 4.wc -c中,-c为统计输出内容的字符数,所以这行命令可以统计前面每一行的字符数并输出 |
| |
| 完整的格式为: |
| a = sed "s#[a-z]##g" user.log |
| while IFS = read -r a; |
| do echo -n "$a" | wc -c |
| done |
4.9.7 sed模式+动作:替换指定行
案例168. 将第3行的sbin替换成renpengyu
| [root@kylin-ren-class ren] |
| root:x:0:0:root:/root:/bin/bash |
| bin:x:1:1:bin:/bin:/sbin/nologin |
| daemon:x:2:2:daemon:/ren:/ren/nologin |
案例169. 将第四行的sbin替换成renpengyu并且取消默认输出
| [root@kylin-ren-class ren] |
| [root@kylin-ren-class ren] |
| adm:x:3:4:adm:/var/adm:/renpengyu/nologin |
案例170. 将文件中的3-6行的sbin替换成renpengyu
| [root@kylin-ren-class ren] |
| daemon:x:2:2:daemon:/renpengyu:/renpengyu/nologin |
| adm:x:3:4:adm:/var/adm:/renpengyu/nologin |
| lp:x:4:7:lp:/var/spool/lpd:/renpengyu/nologin |
| sync:x:5:0:sync:/renpengyu:/bin/sync |
4.9.8 sed模式+动作 模糊过滤
案例170. 查找包含root的行
| [root@kylin-ren-class ren] |
| root:x:0:0:root:/root:/bin/bash |
| operator:x:11:0:operator:/root:/sbin/nologin |
案例171. 包含root的行中的sbin替换为renpengyu
| [root@kylin-ren-class ren] |
| operator:x:11:0:operator:/root:/renpengyu/nologin |
| |
| [root@kylin-ren-class ren] |
| root:x:0:0:root:/root:/bin/bash |
| operator:x:11:0:operator:/root:/renpengyu/nologin |
案例172. 使用边界替换,企业常用案例:将3-7行进行注释
| [root@kylin-ren-class ren]# sed -n '3,7s#^#\##gp' user.log |
| #daemon:x:2:2:daemon:/sbin:/sbin/nologin |
| #adm:x:3:4:adm:/var/adm:/sbin/nologin |
| #lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin |
| #sync:x:5:0:sync:/sbin:/bin/sync |
| #shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown |
4.9.9 sed向后引用
案例173. 输出 “shell” 匹配shell中的字符 "sh" "ell"
| [root@kylin-ren-class ren] |
| ell |
案例174. 标准化输出 ”shell” “mysql“
| [root@kylin-ren-class ren]# echo shellmysql | sed -r 's#(shell)(mysql)#<\1> <\2>#g' |
| <shell> <mysql> |
案例175. 取出IP地址
| [root@kylin-ren-class ren] |
| 10.0.0.210/24 |
案例176. 批量添加用户
| 1.用户拼接 |
| echo ren{1..3} |
| |
| 2.按列显示 |
| echo ren{1..3} | xargs -n1 |
| |
| 3.向后引用 |
| [root@kylin-ren-class ren]# echo ren{1..3} | xargs -n1 | sed -r 's#(.*)#useradd \1#g'|bash |
| |
| 4.过滤刚创建的内容 |
| [root@kylin-ren-class ren] |
| ren1:x:1004:1005::/home/ren1:/bin/bash |
| ren2:x:1005:1006::/home/ren2:/bin/bash |
| ren3:x:1006:1007::/home/ren3:/bin/bash |
案例177. 运算从1加到100
| [root@kylin-ren-class ren]# echo {1..100} | sed 's# #+#g'|bc |
| 5050 |
5.0三剑客之awk(取行,模糊搜索,取列,字符串对比,数字比较)
5.0.1 awk取行 NR == 、>、>=、<、<=、!=、&&、|| 行号
| 语法结构: |
| awk 'NR==[行号]' file |
| NR |
| 符号: |
| == 等于第几行 |
| > 大于第几行 |
| >= 大于等于第几行 |
| < 小于第几行 |
| <= 小于等于第几行 |
| != 不等 |
| && 并且 类似于sed '1,3' |
| || 或者 |
| 准备文件: |
| [root@kylin-ren-class ren] |
| 1 root:x:0:0:root:/root:/bin/bash |
| 2 bin:x:1:1:bin:/bin:/sbin/nologin |
| 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin |
| 4 adm:x:3:4:adm:/var/adm:/sbin/nologin |
| 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin |
| 6 sync:x:5:0:sync:/sbin:/bin/sync |
| 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown |
| 8 halt:x:7:0:halt:/sbin:/sbin/halt |
| 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin |
| 10 operator:x:11:0:operator:/root:/sbin/nologin |
案例178. 取出文件的第三行
| [root@kylin-ren-class ren] |
| daemon:x:2:2:daemon:/sbin:/sbin/nologin |
案例179. 取出文件行数大于7的行
| [root@kylin-ren-class ren] |
| halt:x:7:0:halt:/sbin:/sbin/halt |
| mail:x:8:12:mail:/var/spool/mail:/sbin/nologin |
| operator:x:11:0:operator:/root:/sbin/nologin |
案例180. 取出文件行数大于等于7的行
| [root@kylin-ren-class ren] |
| shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown |
| halt:x:7:0:halt:/sbin:/sbin/halt |
| mail:x:8:12:mail:/var/spool/mail:/sbin/nologin |
| operator:x:11:0:operator:/root:/sbin/nologin |
案例181. 取出文件行数小于3的行
| [root@kylin-ren-class ren] |
| root:x:0:0:root:/root:/bin/bash |
| bin:x:1:1:bin:/bin:/sbin/nologin |
案例182. 取出文件行数小于等于3的行
| [root@kylin-ren-class ren] |
| root:x:0:0:root:/root:/bin/bash |
| bin:x:1:1:bin:/bin:/sbin/nologin |
| daemon:x:2:2:daemon:/sbin:/sbin/nologin |
案例183. 取出文件行数不等于5行的行
| [root@kylin-ren-class ren] |
| root:x:0:0:root:/root:/bin/bash |
| bin:x:1:1:bin:/bin:/sbin/nologin |
| daemon:x:2:2:daemon:/sbin:/sbin/nologin |
| adm:x:3:4:adm:/var/adm:/sbin/nologin |
| sync:x:5:0:sync:/sbin:/bin/sync |
| shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown |
| halt:x:7:0:halt:/sbin:/sbin/halt |
| mail:x:8:12:mail:/var/spool/mail:/sbin/nologin |
| operator:x:11:0:operator:/root:/sbin/nologin |
案例184. 取出文件行数大于3且小于5的行
| [root@kylin-ren-class ren] |
| adm:x:3:4:adm:/var/adm:/sbin/nologin |
| lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin |
案例185. 取出文件第3行或者第5行
| [root@kylin-ren-class ren] |
| daemon:x:2:2:daemon:/sbin:/sbin/nologin |
| lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin |
5.0.2 awk模糊搜索 /字符串/ 、/字符串/,/字符串/
| 语法结构:支持正则 |
| awk '//' file |
| awk '//,//' file |
案例186. 找出包含root的行
| [root@kylin-ren-class ren] |
| root:x:0:0:root:/root:/bin/bash |
| operator:x:11:0:operator:/root:/sbin/nologin |
案例187. 查找包含bash的行
| [root@kylin-ren-class ren] |
| root:x:0:0:root:/root:/bin/bash |
案例188. 查找出以a开头的行
| [root@kylin-ren-class ren] |
| adm:x:3:4:adm:/var/adm:/sbin/nologin |
案例189. 查找文件中包含root或是adm的行
| [root@kylin-ren-class ren] |
| root:x:0:0:root:/root:/bin/bash |
| adm:x:3:4:adm:/var/adm:/sbin/nologin |
| operator:x:11:0:operator:/root:/sbin/nologin |
案例190. 查找中包含b或s开头的行
| [root@kylin-ren-class ren] |
| bin:x:1:1:bin:/bin:/sbin/nologin |
| sync:x:5:0:sync:/sbin:/bin/sync |
| shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown |
案例191. 过滤root开头的行,到adm开头的行
| [root@kylin-ren-class ren] |
| root:x:0:0:root:/root:/bin/bash |
| bin:x:1:1:bin:/bin:/sbin/nologin |
| daemon:x:2:2:daemon:/sbin:/sbin/nologin |
| adm:x:3:4:adm:/var/adm:/sbin/nologin |
案例192. 企业案例:过滤df -h中从最后一个字符为run,到最后一个字符为/的行
| [root@kylin-ren-class ren] |
| tmpfs 979M 8.9M 970M 1% /run |
| tmpfs 979M 0 979M 0% /sys/fs/cgroup |
| /dev/mapper/klas-root 47G 3.9G 44G 9% / |
5.0.3 awk取列 {print $[列号]}
| 语法结构: |
| 默认按照空格或者Tab键分割成列 |
| |
| 如果没有空格或者Tab键,awk会将整行看成一列 |
| |
| 在awk中一切在动作中的字符串都被看做是变量,加上双引号则视为普通的字符串 |
| 准备文件: |
| [root@kylin-ren-class ren] |
| 文件系统 容量 已用 可用 已用% 挂载点 |
| devtmpfs 963M 0 963M 0% /dev |
| tmpfs 979M 0 979M 0% /dev/shm |
| tmpfs 979M 8.9M 970M 1% /run |
| tmpfs 979M 0 979M 0% /sys/fs/cgroup |
| /dev/mapper/klas-root 47G 3.9G 44G 9% / |
| tmpfs 979M 4.0K 979M 1% /tmp |
| /dev/sda1 1014M 169M 846M 17% /boot |
| tmpfs 196M 0 196M 0% /run/user/0 |
| |
| [root@kylin-ren-class ren] |
| a b c d e f g |
| h i g k l m n |
| o p q r s t u |
| v w x y z |
案例193. 取出文件第1列
| [root@kylin-ren-class ren] |
| 文件系统 |
| devtmpfs |
| tmpfs |
| tmpfs |
| tmpfs |
| /dev/mapper/klas-root |
| tmpfs |
| /dev/sda1 |
| tmpfs |
案例194. 取出第1列和第2列
| [root@kylin-ren-class ren]# awk '{print $1,$2}' giaoge.txt |
| a b |
| h i |
| o p |
| v w |
案例195. 双引号被视为普通字符
| [root@kylin-ren-class ren]# awk '{print $1","$2}' giaoge.txt |
| a,b |
| h,i |
| o,p |
| v,w |
| [root@kylin-ren-class ren]# awk '{print $1" 分隔符 "$2}' giaoge.txt |
| a 分隔符 b |
| h 分隔符 i |
| o 分隔符 p |
| v 分隔符 w |
案例196. 取出文件中最后一列,NF是表示每一行有多少列是具体的数字
| [root@kylin-ren-class ren] |
| 7 |
| 7 |
| 7 |
| 5 |
| [root@kylin-ren-class ren] |
| g |
| n |
| u |
| z |
案例197. 取出df -h挂载点那一列内容
| [root@kylin-ren-class ren] |
| 挂载点 |
| /dev |
| /dev/shm |
| /run |
| /sys/fs/cgroup |
| / |
| /tmp |
| /boot |
| /run/user/0 |
案例198. 取出倒数第2列的内容
| [root@kylin-ren-class ren] |
| f |
| m |
| t |
| y |
案例199. 使用awk输出全部内容
| [root@kylin-ren-class ren] |
| a b c d e f g |
| h i g k l m n |
| o p q r s t u |
| v w x y z |
[!CAUTION]
案例200. 将awk处理的内容整理成一行,使用xargs
| [root@kylin-ren-class ren] |
| a |
| h |
| o |
| v |
| |
| |
| [root@kylin-ren-class ren] |
| a h o v |
| 1行4列 |
这里的正常取出的是一列四行的内容,xargs -n后面接的是列号,只要xargs后接的列号大于等于awk的行数即可
5.0.4 awk指定分隔符 -F[分隔符]
| 语法结构: |
| awk -F: '{print $1}' |
| awk -F ":" '{print $1}' |
| 准备文件: |
| [root@kylin-ren-class ren] |
| root:x:0:0:root:/root:/bin/bash |
| bin:x:1:1:bin:/bin:/sbin/nologin |
| daemon:x:2:2:daemon:/sbin:/sbin/nologin |
| adm:x:3:4:adm:/var/adm:/sbin/nologin |
| lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin |
| sync:x:5:0:sync:/sbin:/bin/sync |
| shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown |
| halt:x:7:0:halt:/sbin:/sbin/halt |
| mail:x:8:12:mail:/var/spool/mail:/sbin/nologin |
| operator:x:11:0:operator:/root:/sbin/nologin |
| |
| [root@kylin-ren-class ren] |
| 张三 Linux96 49 |
| 李四 Linux96 90 |
| 王五 Linux96 78 |
| 任六 Linux96 100 |
| 冯七 Linux96 53 |
| 周八 Linux96 69 |
案例200. 取出passwd文件中第一列
| [root@kylin-ren-class ren]# awk -F: '{print $1}' user.log |
| root |
| bin |
| daemon |
| adm |
| lp |
| sync |
| shutdown |
| halt |
| mail |
| operator |
案例201. 统计成绩
| [root@kylin-ren-class ren]# awk -F" " '{print $3}' awk.txt | sort -nr |
| 100 |
| 90 |
| 78 |
| 69 |
| 53 |
| 49 |
案例202. 万物皆可分割
| [root@kylin-ren-class ren]# awk -F ":/" '{print $2}' user.log |
| root |
| bin |
| sbin |
| var/adm |
| var/spool/lpd |
| sbin |
| sbin |
| sbin |
| var/spool/mail |
| root |
[!IMPORTANT]
{print $NF}:显示每一行(不指定分隔符按一列算除了空格Tab键)最后一列的具体内容
{print NF}:显示每一行的最大列数
案例203. 指定两个分隔符
| [root@kylin-ren-class ren]# awk -F ":/" '{print $2}' user.log |
| root |
| bin |
| sbin |
| var/adm |
| var/spool/lpd |
| sbin |
| sbin |
| sbin |
| var/spool/mail |
| root |
案例204. 任意字符作为分隔符
| # 以":" 和 "/" 为分隔符分割成每一列,然后统计每一行有多少列 |
| [root@kylin-ren-class ren]# awk -F "[:/]" '{print NF}' user.log |
| 10 |
| 10 |
| 10 |
| 11 |
| 12 |
| 10 |
| 10 |
| 10 |
| 12 |
| 10 |
案例205. 任意字符为分隔符,但是多个字符在一起算一“刀”
| [root@kylin-ren-class ren]# awk -F "[:/]+" '{print NF}' user.log |
| 8 |
| 8 |
| 8 |
| 9 |
| 10 |
| 8 |
| 8 |
| 8 |
| 10 |
| 8 |
案例206. 使用+号来表示砍括号内字符随机组合的一次及一次以上
| [root@kylin-ren-class ren]# echo -:+/o1dboy-:-/test--/:+ | awk -F "[-:+/]+" '{print NF}' |
| 4 |
[!CAUTION]
[-:+/]+ 是指匹配方括号内随机组合的字符一次及一次以上,划分是算一“刀”
案例207. 统计域名出现的次数
| [root@kylin-ren-class ren]# awk -F "/+" '{print $2}' com.txt | sort | uniq -c | sort -nr -k1 |
| 2 www.sina.com |
| 2 www.baidu.com |
| 1 www.weibo.com |
| 1 www.oldboyedu.com |
案例208,将renpengyu分割成三部分,取出ren eng u
| [root@kylin-ren-class ren]# echo renpengyu | awk -F "[py]+" '{print $1,$2,$3}' |
| ren eng u |
5.0.5 awk模式+动作(找谁干什么)
| 模式: |
| 通过NR找出指定行 awk 'NR == 3' |
| 通过模糊过滤的方式找出行 awk '/root/' |
| |
| awk 'NR == 5{print $1}' |
| 准备内容: |
| [root@kylin-ren-class ren] |
| 1 root:x:0:0:root:/root:/bin/bash |
| 2 bin:x:1:1:bin:/bin:/sbin/nologin |
| 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin |
| 4 adm:x:3:4:adm:/var/adm:/sbin/nologin |
| 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin |
| 6 sync:x:5:0:sync:/sbin:/bin/sync |
| 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown |
| 8 halt:x:7:0:halt:/sbin:/sbin/halt |
| 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin |
| 10 operator:x:11:0:operator:/root:/sbin/nologin |
案例209. 输出文件中第2行第2列
| [root@kylin-ren-class ren]# awk -F : 'NR == 2 {print $2}' user.log |
| x |
案例210. 输出文件中大于第六行的最后一列
| [root@kylin-ren-class ren]# awk -F "/" 'NR > 6 {print $NF}' user.log |
| shutdown |
| halt |
| nologin |
| nologin |
案例211. 输出第一行的第一列和最后一列
| [root@kylin-ren-class ren]# awk -F "[:/]" 'NR ==1 {print $1,$NF}' user.log |
| root bash |
案例212. 输出包含adm的行的第三列
| [root@kylin-ren-class ren]# awk -F "[:/]" '/adm/ {print $3}' user.log |
| 3 |
5.0.6 格式化输出 print “...是:$[列号]”
| 格式化输出: |
| print函数处可以添加字符串用来表明后面的字符的意义,和在变成中的标准化输出一个道理 |
| 文件准备: |
| 准备内容: |
| [root@kylin-ren-class ren] |
| 1 root:x:0:0:root:/root:/bin/bash |
| 2 bin:x:1:1:bin:/bin:/sbin/nologin |
| 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin |
| 4 adm:x:3:4:adm:/var/adm:/sbin/nologin |
| 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin |
| 6 sync:x:5:0:sync:/sbin:/bin/sync |
| 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown |
| 8 halt:x:7:0:halt:/sbin:/sbin/halt |
| 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin |
| 10 operator:x:11:0:operator:/root:/sbin/nologin |
| |
| [root@kylin-ren-class ren] |
| 文件系统 容量 已用 可用 已用% 挂载点 |
| devtmpfs 963M 0 963M 0% /dev |
| tmpfs 979M 0 979M 0% /dev/shm |
| tmpfs 979M 8.9M 970M 1% /run |
| tmpfs 979M 0 979M 0% /sys/fs/cgroup |
| /dev/mapper/klas-root 47G 3.9G 44G 9% / |
| tmpfs 979M 4.0K 979M 1% /tmp |
| /dev/sda1 1014M 169M 846M 17% /boot |
| tmpfs 196M 0 196M 0% /run/user/0 |
案例213. 准备化输出第1行和第5行的用户名
| [root@kylin-ren-class ren]# awk -F ":" 'NR == 1 || NR == 5 {print "用户名是: " $1}' user.log |
| 用户名是: root |
| 用户名是: lp |
案例214. 标准化输出第1行和第5行的用户名和UID的内容
| [root@kylin-ren-class ren]# awk -F ":" 'NR == 1 || NR == 5 {print "用户名是:" $1 "\t UID是:" $3}' user.log |
| 用户名是:root UID是:0 |
| 用户名是:lp UID是:4 |
案例215. 取出磁盘信息中/已用百分比
| [root@kylin-ren-class ren] |
| 9% |
案例216. 在215的案例上,将结果的%去掉
| [root@kylin-ren-class ren]# awk -F "[% /]+" 'NR==6{print $(NF-1)}' df-h.txt |
| 9 |
5.0.7 字符串比对(比较表达式)
| 准备内容: |
| [root@kylin-ren-class ren] |
| 1 root:x:0:0:root:/root:/bin/bash |
| 2 bin:x:1:1:bin:/bin:/sbin/nologin |
| 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin |
| 4 adm:x:3:4:adm:/var/adm:/sbin/nologin |
| 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin |
| 6 sync:x:5:0:sync:/sbin:/bin/sync |
| 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown |
| 8 halt:x:7:0:halt:/sbin:/sbin/halt |
| 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin |
| 10 operator:x:11:0:operator:/root:/sbin/nologin |
| |
| [root@kylin-ren-class ren] |
| 张三 Linux96 49 |
| 李四 Linux96 90 |
| 王五 Linux96 78 |
| 任六 Linux96 100 |
| 冯七 Linux96 53 |
| 周八 Linux96 69 |
案例217. 查找第1列等于root的行
| [root@kylin-ren-class ren] |
| root:x:0:0:root:/root:/bin/bash |
案例218. 取出第1列等于root或者等于adm的行
| [root@kylin-ren-class ren] |
| root:x:0:0:root:/root:/bin/bash |
| adm:x:3:4:adm:/var/adm:/sbin/nologin |
案例219. 取出不等于root的行
| [root@kylin-ren-class ren] |
| bin:x:1:1:bin:/bin:/sbin/nologin |
| daemon:x:2:2:daemon:/sbin:/sbin/nologin |
| adm:x:3:4:adm:/var/adm:/sbin/nologin |
| lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin |
| sync:x:5:0:sync:/sbin:/bin/sync |
| shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown |
| halt:x:7:0:halt:/sbin:/sbin/halt |
| mail:x:8:12:mail:/var/spool/mail:/sbin/nologin |
案例220. 取出最后一列等于bash的行
| [root@kylin-ren-class ren] |
| root:x:0:0:root:/root:/bin/bash |
案例221. 找到最后一列内容是以n开头的字符的行
| [root@kylin-ren-class ren] |
| bin:x:1:1:bin:/bin:/sbin/nologin |
| daemon:x:2:2:daemon:/sbin:/sbin/nologin |
| adm:x:3:4:adm:/var/adm:/sbin/nologin |
| lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin |
| mail:x:8:12:mail:/var/spool/mail:/sbin/nologin |
| operator:x:11:0:operator:/root:/sbin/nologin |
案例222. 找到匹配第三列能匹配3或5的行
| [root@kylin-ren-class ren] |
| adm:x:3:4:adm:/var/adm:/sbin/nologin |
| sync:x:5:0:sync:/sbin:/bin/sync |
案例223. 使用正则匹配字符串,匹配第一列以n结尾的行
| [root@kylin-ren-class ren] |
| bin:x:1:1:bin:/bin:/sbin/nologin |
| daemon:x:2:2:daemon:/sbin:/sbin/nologin |
| shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown |
案例224. 取出不是r开头的行
| operator:x:11:0:operator:/root:/sbin/nologin |
| [root@kylin-ren-class ren] |
| bin:x:1:1:bin:/bin:/sbin/nologin |
| daemon:x:2:2:daemon:/sbin:/sbin/nologin |
| adm:x:3:4:adm:/var/adm:/sbin/nologin |
| lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin |
| sync:x:5:0:sync:/sbin:/bin/sync |
| shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown |
| halt:x:7:0:halt:/sbin:/sbin/halt |
| mail:x:8:12:mail:/var/spool/mail:/sbin/nologin |
| operator:x:11:0:operator:/root:/sbin/nologin |
[!CAUTION]
[^r]
是指以r为开头的行
^[^r]
是指不要开头为r的行
5.0.8 awk数字比较
| 文件准备: |
| [root@kylin-ren-class ren] |
| 张三 Linux96 49 |
| 李四 Linux96 90 |
| 王五 Linux96 78 |
| 任六 Linux96 100 |
| 冯七 Linux96 53 |
| 周八 Linux96 69 |
| |
| [root@kylin-ren-class ren] |
| 张三 Linux96 49 30 |
| 李四 Linux96 90 28 |
| 王五 Linux96 78 69 |
| 任六 Linux96 100 50 |
| 冯七 Linux96 53 39 |
| 周八 Linux96 69 96 |
案例225. 将第3列大于100的行取出来
| [root@kylin-ren-class ren] |
| 任六 Linux96 100 |
案例226. 将第3列小于100且大于50的列取出来
| [root@kylin-ren-class ren]# awk '$3 < 100 && $3 > 50' awk.txt |
| 李四 Linux96 90 28 |
| 王五 Linux96 78 69 |
| 冯七 Linux96 53 39 |
| 周八 Linux96 69 96 |
案例227. 企业案例:统计分数大于60分的同学有几个
| [root@kylin-ren-class ren] |
| 王五 Linux96 78 69 |
| 周八 Linux96 69 96 |
案例228. 统计第4列分数大于70的同学有几个
| [root@kylin-ren-class ren] |
| 周八 Linux96 69 96 |
[!CAUTION]
| $NF |
| |
| $NF-1 |
| |
| awk '{print 10 + 1}' |
5.0.9 awk的扩展之 ---BEGIN/END---
BEGIN模式:是指 awk 将在读取任何输入行之前立即执行 BENGIN
中指定的动作。
END模式:是指 awk 将在它正式退出前执行 END
中指定的动作。
回顾一下awk的通用语言:
以及awk脚本拥有这样的形式:
所以将代码改成 BEGIN和END形式则为:
| awk ' |
| BEGIN { actions } |
| /pattern/ { actions } |
| /parttern/ { actions } |
| ... |
| END { actions } |
| ' filenames |
含有特殊模式的执行流程:
案例299. 现有一个存有域名domains.txt文件。希望统计出 domains.txt 文件中域名tecmint.com 出现的次数。
| news.tecmint.com |
| tecmint.com |
| linuxsay.com |
| windows.tecmint.com |
| tecmint.com |
| news tecmint com |
| tecmint.com |
| linuxsay.com |
| tecmint com |
| news.tecmint.com |
| tecmint.com |
| linuxsay.com |
| windows.tecmint.com |
| tecmint.com |
| for file in $@; do |
| if [ -f $file ]; then |
| |
| echo "File is : $file" |
| |
| awk '/^tcemint.com/ {counter+=1 ; printf "%s/n, counter ;" }' $file |
| else |
| |
| echo "$file 不是一个文件,请指定一个文件。" >&2 && exit 1 |
| fi |
| done |
| |
| exit 0 |
加入两个模式后:
| for file in $@; do |
| if [ -f $file ];then |
| |
| echo "File is : $file" |
| |
| awk ' BENGIN { print "文件中出现 tecmint.com 的次数是:" ; } |
| /^tecmint.com/ {counter+=1 ; } |
| END { print "%s/n", counter ; } |
| ' $file |
| else |
| |
| echo "$file 不是一个文件,请指定一个文件。" >&2 && exit 1 |
| fi |
| done |
| |
| exit 0 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步