shell 基础练习笔记——awk 使用

这篇博客详细介绍了awk命令的使用,包括以冒号为分隔符打印文件内容、匹配特定模式、条件判断以及输出格式化等。通过示例展示了awk在处理文本文件时的强大功能,适合初学者巩固awk基础知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

awk -F ':' '{print $1}' passwd  以冒号为分隔符,打印passwd文件的第一段

 

 

awk -F ':' '{print $1}' 1.txt

  awk -F ':' '{print $1}' inittab

  awk -F ':' '{print $1}' passwd

  awk '{print $0}' passwd

  awk '{print $1,$2}' passwd

  awk -F ':' '{print $1,$2}' passwd

  awk -F ':' '{print $1,$4}' passwd

  awk -F ':' '{print $1"hahha"$4}' passwd

  awk -F ':' '{print $1"*"$4}' passwd

  awk '/oo/' passwd

  awk -F ':'  '$1~/oo/' passwd ~

  awk -F ':'  '$1 ~ /oo/' passwd ~

  awk -F ':'  '$1 ~ /oo/' passwd

  awk -F ':'  '$2 ~ /oo/' passwd

  awk -F ':'  '$6 ~ /oo/' passwd

  awk -F ':' '/root/ {print $1,$2} /user/'

  awk -F ':' '/root/ {print $1,$2} /user/ {print $1,$3}' passwd

  awk '{print $0}' passwd

  awk -F ':' '/root/ {print $1,$2} /run/ {print $1,$3}' passwd

  awk -F ':' '/root/ {print $1,$2} /run/ {print $0}' passwd

  awk -F ':' '$3==0 {print $1}'pa

  awk -F ':' '$3==0 {print $1}' passwd

  awk -F ':' '$3>=500 {print $1}' passwd

  awk -F ':' '$3>="500" {print $1}' passwd

  awk -F ':' '$3>=500 {print $1}' passwd

  awk -F ':' '$7!="/sbin/nologin" {print $1}' passwd

  awk -F ':' '$7!="/sbin/nologin" ' passwd

  awk -F ':' '$7!="/sbin/nologin" {print $0} ' passwd

  awk -F ':' '$7!="/usr/sbin/nologin" {print $0} ' passwd

 

 

  注意事项:数字比对的时候  不要加 “双引号”“”

                 分段中的$0 表示 全部的内容

                不指定分隔符,默认为空格 或者空白字符 

 

 

awk -F ':' '$3<$4' passwd

awk -F ':' '$3==$4' passwd

awk -F ':' '$3>"5"&&$3<"7"' passwd

 

 

awk#  awk -F ':' '{OFS="#+#"} $3>1000||$7 ~ /bash/ {print $1,$3,$7}' tt.txt

 

 

awk -F ':' '{OFS="#+#"} {print $1,$3}' tt.txt

awk -F ':' '{OFS="#+#"} $3>1000||$7 ~ /bash/ {print $1,$3}' tt.txt

awk -F ':' '{OFS="#+#"} $3>1000||$7 ~ /bash/ {print $1,$3,$7}' tt.txt

awk -F ':' '{OFS="#+#"} $3>1000||$7 ~ /bash/ {print $0}' tt.txt

awk -F ':' '{OFS="#+#"} {print $1,$3,$7}' tt.txt

awk -F ':' '{OFS="#+#"}{if ($3>1000) {print $1,$3,$7}}' tt.txt

awk -F ':' '{OFS="#+#"} $3>1000 {print $1,$3,$7}' tt.txt

 

 

 

 

awk -F ':' '{(tot=tot+$3)}; END {print tot}' tt.txt    tot求和

分段 tot=tot+$3  tot值每次循环加第三段值 第一次 为0 不存在 默认tot不存在为0 ;第二次为 第一行的第三段+第二行的第三段
 

第三段所有的和 ,相当于求一列值

 

 

不断尝试,不断练习

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值