awk
[root@koukou tan_shell]# df -Th #文件系统 类型 容量 已用 可用 已用%% 挂载点 /dev/sda3 ext4 321G 223G 83G 73% / tmpfs tmpfs 1.9G 608K 1.9G 1% /dev/shm /dev/sda1 ext4 92G 261M 87G 1% /boot [root@koukou...
awk
[root@koukou tan_shell]# df -Th #文件系统 类型 容量 已用 可用 已用%% 挂载点 /dev/sda3 ext4 321G 223G 83G 73% / tmpfs tmpfs 1.9G 608K 1.9G 1% /dev/shm /dev/sda1 ext4 92G 261M 87G 1% /boot [root@koukou tan_shell]# df -Th | grep "/$" /dev/sda3 ext4 321G 223G 83G 73% / [root@koukou tan_shell]# df -Th | grep "/$"|awk '{print $5}' 83G [root@koukou tan_shell]# df -Th | grep "/$"|awk '{print $6}' 73% [root@koukou tan_shell]# df -Th | grep "/$"|awk '{print $6}'|tr -d "%" 73 [root@koukou tan_shell]# [root@koukou mysql]# df -t ext4 #文件系统 1K-块 已用 可用 已用% 挂载点 /dev/sda3 336503968 243882004 75528488 77% / /dev/sda1 96143116 266440 90992812 1% /boot [root@koukou mysql]# df -t ext4|awk 'NR==1' #输出第一行 #文件系统 1K-块 已用 可用 已用% 挂载点 [root@koukou mysql]# df -t ext4|awk 'END{print $0}' #输出最后一行 /dev/sda1 96143116 266440 90992812 1% /boot [root@koukou mysql]#
几种截取某个字符的方法:
[root@koukou tan_shell]# echo "abc" |rev|cut -c1 c [root@koukou tan_shell]# a="abc" [root@koukou tan_shell]# echo ${a: -1} c [root@koukou tan_shell]# echo "abc" |awk -F "" '{print $1}' a [root@koukou tan_shell]# echo "abc" |awk -F "" '{print $NF}' #若要以空格为分割副则这里双引号内必须有空格 c [root@koukou tan_shell]#
几种截取百分比的方法:
[root@koukou tan_shell]# df -Th|grep "/$"|awk '{print $6}' 74% [root@koukou tan_shell]# df -Th|grep "/$"|awk '{print $6}'|tr -d "%" 74 [root@koukou tan_shell]# df -Th|grep "/$"|awk '{print $6}'|sed 's/%//' 74 [root@koukou tan_shell]# df -Th|grep "/$"|awk '{print int($6)}' 74 [root@koukou tan_shell]# df -Th|grep "/$"|awk '{print int(3.1415)}' 3 [root@koukou tan_shell]# [root@koukou tan_shell]# val=this [root@koukou tan_shell]# echo "$val is %" this is % [root@koukou tan_shell]# echo "$valis %" % [root@koukou tan_shell]# echo "${val}is %" thisis % [root@koukou tan_shell]# echo "${val} 'is %" this 'is % [root@koukou tan_shell]#
awk命令内插入其他命令的使用方法:
[root@tan anli]# awk '{ system("ls -l") }' test1.sh #有多少行执行几次 [root@tan anli]# awk '{ system("useradd" $3) }' test1.sh #每一行的第三列作为用户名useradd添加用户 [root@tan anli]# cat file yi1 yi2 yi3 yi4 er1 er2 er3 er4 san1 san2 san3 san4 [root@tan anli]# awk '{ system("pwd ")}' file /tan_shell/anli /tan_shell/anli /tan_shell/anli [root@tan anli]# [root@tan anli]# awk '{ system("echo " $1)}' file yi1 er1 san1 [root@tan anli]#
打印某一行:
[root@tan tan_shell]# awk "NR==1" /etc/passwd root:x:0:0:root:/root:/bin/bash [root@tan tan_shell]# awk "NR==2" /etc/passwd bin:x:1:1:bin:/bin:/sbin/nologin [root@tan tan_shell]#
打印最后一个字段
[root@koukou boot]# ls -l|awk '$5>=102{print $NF}' 输出大小大于等于102的文件名 #取最后一个字符 echo ${a: -1} echo $a | rev | cut -c1 #查看文件的第几行: sed -n '5p' file1 awk 'NR==5' file1 head -5 file1 | tail -1
打印file文件内空行所在的行号: [root@localhost tan]# awk '{if($0~/^$/)print NR}' file 2 4 [root@localhost tan]# vim file [root@localhost tan]# cat file sadasdsadas sadjh asjdh
方法二:
[root@localhost tan]# grep -n ^$ file -n#是显示行号 2: 4: [root@localhost tan]# grep -n ^a file 5:asjdh [root@localhost tan]# cat file sadasdsadas sadjh asjdh [root@localhost tan]#
由上面可以看出,行号已经显示出来了,现在只需要提取出行号就ok了
[root@localhost tan]# grep -n ^$ file|awk -F ":" "{print $1}" //错误做法,不能用双引号 2: 4: [root@localhost tan]# grep -n ^$ file|awk -F":" '{print $1}' 2 4 [root@localhost tan]# [root@localhost tan]# grep -n ^$ file|awk 'BEGIN{FS=":"}{print $1}' 2 4 [root@localhost tan]# [root@localhost tan]# grep -n ^$ file|awk -F: 'BEGIN{FS=":"}{print $1}' 2 4 [root@localhost tan]# BEGIN{...}.....END{..........} #语句块
该语句执行不要求从文件读取数据
[root@koukou tan_shell]# route -n |grep "UG"|awk '{print $2}' 10.0.1.254 [root@koukou tan_shell]# awk -F: 'BEGIN{print "start"}' /etc/passwd start [root@koukou tan_shell]# awk -F: 'BEGIN{print "start"}' 输出开始字符串 start [root@koukou tan_shell]# awk -F: 'BEGIN{print start}' [root@koukou tan_shell]# awk -F: 'BEGIN{start=10;print start}' 不加双引号的话就认为是变量 10 [root@koukou tan_shell]# [root@koukou tan_shell]# awk -F: 'BEGIN{print "hello!!!"}{print $1}' test.txt hello!!! abchsafkjhd def gjtshfajas def abc abc haha [root@koukou tan_shell]# awk -F: 'BEGIN{print "hello!!!"}{print $1}' hello!!! test.txt test.txt afjk afjk safd fads safd fads ^C [root@koukou tan_shell]#
模式匹配,打印输出:
[root@koukou tan_shell]# awk -F: '/root/{print $1}' /etc/passwdroot operator [root@koukou tan_shell]# awk -F: '/^root/{print $1}' /etc/passwd root [root@koukou tan_shell]# awk -F: '/bash/{print $1}' /etc/passwd root mysql lizhenhua weichao tan tan1 tan2 tan3 js1 kf1 js2 kf2 JECK tom jerry lee [root@koukou tan_shell]# awk -F: '/bash$/{print $1}' /etc/passwd root mysql lizhenhua weichao tan tan1 tan2 tan3 js1 kf1 js2 kf2 JECK tom jerry lee [root@koukou tan_shell]# [root@koukou tan_shell]# awk -F: '/root/' /etc/passwd #默认打印出整行 root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@koukou tan_shell]#
BEGIN里面的FS:相当于-F的功能
[root@koukou tan_shell]# awk 'BEGIN{FS=":"}NR<=5{print $1,NR}' /etc/passwd FS:#分割符,NR行号 root 1 bin 2 daemon 3 adm 4 lp 5 [root@koukou tan_shell]# [root@koukou tan_shell]# awk 'BEGIN{FS=" "}NR<=5{print $1,NR}' /etc/passwd root:x:0:0:root:/root:/bin/bash 1 bin:x:1:1:bin:/bin:/sbin/nologin 2 daemon:x:2:2:daemon:/sbin:/sbin/nologin 3 adm:x:3:4:adm:/var/adm:/sbin/nologin 4 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 5 [root@koukou tan_shell]#
OFS: {print $1, NR}的分割符,默认为一个空格
[root@koukou tan_shell]# awk 'BEGIN{FS=":"; OFS="--------"}NR<=5{print $1, NR}' /etc/passwd root--------1 bin--------2 daemon--------3 adm--------4 lp--------5 [root@koukou tan_shell]# [root@koukou tan_shell]# awk 'BEGIN{FS=":"; OFS="tttt"}NR<=5{print $1, NR}' /etc/passwd root 1 bin 2 daemon 3 adm 4 lp 5 [root@koukou tan_shell]# [root@koukou tan_shell]# echo "abc hij:xyz 123 456:qq" |awk -F"[ t:]+" '{print $3}' 分割符为空格或者t xyz [root@koukou tan_shell]# echo "abc hij:xyz 123 456:qq" |awk -F"[ :]+" '{print $3}' xyz [root@koukou tan_shell]# awk 'BEGIN{a=10; b=12 ;print a+b}' 22 [root@koukou tan_shell]# echo "abc hij:xyz 123 456:qq" |awk -F: '{print $3}' qq
计算总的行数:
[root@koukou tan_shell]# cat test.txt abchsafkjhd tom saf asfs cfh kjj [root@koukou tan_shell]# awk '{print NR}' test.txt 1 2 3 4 5 [root@koukou tan_shell]# awk 'END{print NR}' /etc/passwd 54 [root@koukou tan_shell]# awk 'END{print NR}' test.txt 5 [root@koukou tan_shell]# awk 'BEGIN{print NR}' test.txt 0 [root@koukou tan_shell]# [root@koukou tan_shell]# awk 'BEGIN{i=0}{i++}END{print i}' test.txt 5 [root@koukou tan_shell]# awk 'BEGIN{i=0}{i++}{print i}' test.txt 1 2 3 4 5 [root@koukou tan_shell]# [root@koukou tan_shell]# awk 'BEGIN{i=0}{i++;print i}' test.txt 1 2 3 4 5 [root@koukou tan_shell]# [root@koukou tan_shell]# awk -F: '$3==509{print $1}' /etc/passwd js1 [root@koukou tan_shell]# [root@koukou tan_shell]# awk -F: '$3+$4>=5000{print $1}' /etc/passwd nfsnobody [root@koukou tan_shell]# [root@koukou tan_shell]# awk -F: '$1=="root"{print $1}' /etc/passwd #匹配字符串 root [root@koukou tan_shell]# awk -F: '$1=="root"{print $3}' /etc/passwd 0 [root@koukou tan_shell]# awk -F: '$1=="root"{print $4}' /etc/passwd 0 [root@koukou tan_shell]# awk -F: '$1=="root"{print $0}' /etc/passwd root:x:0:0:root:/root:/bin/bash [root@koukou tan_shell]#
-v选项 接受变量
[root@koukou tan_shell]# awk '$1~/root/{print $0}' /etc/passwd #~匹配,每一行是否包含有root,有则输出整行 root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@koukou tan_shell]# awk '$0~/root/{print $1}' /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@koukou tan_shell]# awk -F:'$0~/root/{print $1}' /etc/passwd ^C [root@koukou tan_shell]# awk -F: '$0~/root/{print $1}' /etc/passwd root operator [root@koukou tan_shell]# [root@koukou tan_shell]# a=1 [root@koukou tan_shell]# b=2 [root@koukou tan_shell]# awk -v v1=$a -v v2=$b 'BEGIN{print v1/v2}' 0.5 [root@koukou tan_shell]# c=`awk -v v1=$a -v v2=$b 'BEGIN{print v1/v2}'` [root@koukou tan_shell]# echo $c 0.5 [root@koukou tan_shell]#
中英文切换:
LANG=C C即ascii, 或LANG=也可, 自动使用英文 vim /etc/sysconfig/i18n echo $LANG LANG="en_US.UTF-8" LANG="zh_CN.UTF-8" [root@koukou 桌面]# echo $LANG zh_CN.UTF-8 [root@koukou 桌面]# [root@koukou 桌面]# LANG= [root@koukou 桌面]# echo $LANG [root@koukou 桌面]# [root@koukou tan_shell]# c=`awk 'BEGIN{print a/b}'` awk: 致命错误: 试图除0 [root@koukou tan_shell]# c=`awk 'BEGIN{a=1;b=2;print a/b}'` [root@koukou tan_shell]# echo $c 0.5 [root@koukou tan_shell]# LANG= #这样设置一下,错误提示就变为英文的了 [root@koukou tan_shell]# c=`awk 'BEGIN{print a/b}'` awk: fatal: division by zero attempted [root@koukou tan_shell]# [root@koukou tan_shell]# ifconfig -a | grep "^w" #字母数字下划线开头的 eth0 Link encap:Ethernet HWaddr 00:30:67:F2:0C:21 eth1 Link encap:Ethernet HWaddr EC:88:8F:EF:3D:03 lo Link encap:Local Loopback vmnet1 Link encap:Ethernet HWaddr 00:50:56:C0:00:01 vmnet2 Link encap:Ethernet HWaddr 00:50:56:C0:00:02 vmnet3 Link encap:Ethernet HWaddr 00:50:56:C0:00:03 vmnet8 Link encap:Ethernet HWaddr 00:50:56:C0:00:08 [root@koukou tan_shell]#
模式匹配:
[root@koukou tan_shell]# awk -F: '/root/' /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@koukou tan_shell]# awk -F: '/root/{print $0}' /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@koukou tan_shell]# awk 'NR==4' /etc/passwd adm:x:3:4:adm:/var/adm:/sbin/nologin [root@koukou tan_shell]#
判断:
[root@koukou tan_shell]# awk -F: '{if($0 ~ /^root/)print $1}' /etc/passwd root [root@koukou tan_shell]# awk -F: '/root/{if($0 ~ /^root/)print $1}' /etc/passwd root [root@koukou tan_shell]# awk -F: 'NR<=10{if($3==501)print $3;else if($3==502)print NR;else print NF}' /etc/passwd 7 7 7 7 7 7 7 7 7 7 [root@koukou tan_shell]# [root@koukou tan_shell]# awk -F: '{if($1 == "root" || $3 == 503)print "ok"}' /etc/passwd ok [root@koukou tan_shell]# awk -F: '{if($1 ~ /^root/ || $3 == 503)print "ok"}' /etc/passwd ok [root@koukou tan_shell]# awk -F: '{if(NR>3)next;print $1}END{}print NR}' /etc/passwd awk: {if(NR>3)next;print $1}END{}print NR} awk: ^ syntax error [root@koukou tan_shell]# awk -F: '{if(NR>3)next;print $1}END{print NR}' /etc/passwd root bin daemon 54 [root@koukou tan_shell]# awk -F: '{if(NR>3){next;}; print $1}END{print NR}' /etc/passwd #没包含在判断语句内还是不会读取下一行,可见next的作用是读取下一行,不执行print$ root bin daemon 54 [root@koukou tan_shell]# [root@koukou tan_shell]# awk -F: '{if(NR>3){exit;}; print $1}END{print NR}' /etc/passwd exit#跳出循环,执行END后面的语句 root bin daemon 4 [root@koukou tan_shell]# [root@koukou tan_shell]# awk 'BEGIN{for(i=1;i<=5;i++)print i}' 1 2 3 4 5 [root@koukou tan_shell]# [root@koukou tan_shell]# df -Th |grep "/$"|awk '{print int($6)}' 74 [root@koukou tan_shell]# df -Th |grep "/$"|awk '{print int(4.222}' awk: {print int(4.222} awk: ^ syntax error [root@koukou tan_shell]# df -Th |grep "/$"|awk '{print int(4.222)}' 4 [root@koukou tan_shell]#
printf:取小数精度
[root@koukou tan_shell]# df -Th |grep "/$"|awk '{printf "%.2fn",3.3333}' 3.33 [root@koukou tan_shell]# df -Th |grep "/$"|awk '{printf "%.3fn",3.3333}' 3.333 [root@koukou tan_shell]#
system函数调用,实现在awk内部调用命令
[root@koukou tan_shell]# cat test.txt abchsafkjhd tom [root@koukou tan_shell]# awk '{system("useradd " $2)}' test.txt useradd后面要空格 useradd: user 'tom' already exists [root@koukou tan_shell]# userdel -r tom userdel: /var/spool/mail/tom not owned by tom, not removing [root@koukou tan_shell]# awk '{system("useradd " $2)}' test.txt Creating mailbox file: File exists [root@koukou tan_shell]# $1 ,$0,NR,NF,$NF,$(NF-1)的意思:
$1是第一个字段,$0是整行,N是行号,NF是是最后一个字段的位置,$NF是最后一个字段的内容
[root@koukou tan_shell]# awk -F: 'BEGIN{OFS="tt"}$3<=5{print $1 ,$0,NR,NF,$NF,$(NF-1)}' /etc/passwd root root:x:0:0:root:/root:/bin/bash 1 7 /bin/bash /root bin bin:x:1:1:bin:/bin:/sbin/nologin 2 7 /sbin/nologin /bin daemon daemon:x:2:2:daemon:/sbin:/sbin/nologin 3 7 /sbin/nologin /sbin adm adm:x:3:4:adm:/var/adm:/sbin/nologin 4 7 /sbin/nologin /var/adm lp lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 5 7 /sbin/nologin /var/spool/lpd sync sync:x:5:0:sync:/sbin:/bin/sync 6 7 /bin/sync /sbin [root@koukou tan_shell]# awk -F: 'BEGIN{OFS="tt"}$3<=5' /etc/passwd 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 [root@koukou tan_shell]# seq [root@koukou 桌面]# seq 5 1 2 3 4 5 [root@koukou 桌面]# seq -w 5 1 2 3 4 5 [root@koukou 桌面]# seq -w 10 以最大的等宽显示 01 02 03 04 05 06 07 08 09 10 [root@koukou 桌面]# [root@koukou tan_shell]# seq -s* 10 1*2*3*4*5*6*7*8*9*10 [root@koukou tan_shell]# seq -s* 10|bc 求10! 3628800 [root@koukou tan_shell]# [root@koukou ~]# seq -s+ 1 2 10|bc 25 [root@koukou ~]# seq -s+ 1 2 10 从1开始,隔两个数取一个,取到10 1+3+5+7+9 [root@koukou ~]# [root@koukou mysql]# seq 10|tr -d "59" 1 2 3 4 6 7 8 10 [root@koukou mysql]# seq 10|tr -d "59"|grep -v "^$" 去掉空行 1 2 3 4 6 7 8 10
本文出自: http://blog.csdn.net/sxkjkoukou/article/details/14456263

关注公众号『窗外天空』
获取更多建站运营运维新知!互联网创业、前沿技术......
最新评论
水淀粉vdfv
有其他下载方式么,网站上的点击下载后没有任何反应,或者直接发给我一下?83835079@qq.com
你好,我的型号ELECOM WRC-X3200GST3,ARMv8 Processor rev 4构架,CPU mediatek/mt7622,找了很久没有找到
我的也是这样。一直无法确认ARCH架构,或是不支持。一直没办法用。不知道怎么办了
您好,现在安装上了,可是ssr plus+配置好节点也没用,一直都是未运行,节点是有效的; 另外那个passwall2一找开就提示"无法确认ARCH架构,或是不支持", 麻烦大佬帮忙看下是什么问题,谢谢!