Linux 文本处理工具awk
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux 文本处理工具awk相关的知识,希望对你有一定的参考价值。
很好用的文本处理工具,尤其是变量跟控制语句,使用超赞。
个人理解大致流程如下
1、正常输出
# $0表示正行 默认是按照行分割 $1 $2
#-----------------------------------------------------------------
[[email protected] test]# awk ‘{print "hello ",$2}‘ fstab
hello
hello
hello /etc/fstab
hello Created
2、变量
#FS:input field seperator, 输入 在行里面里面的分割符,默认为空白字符;
#OFS:output field seperator,输出 在行里面里面的分割符,默认为空白字符;
#-----------------------------------------------------------------
[[email protected] test]# awk -F‘ ‘ ‘{print $4}‘ fstab
[[email protected] test]# awk -v FS=‘ ‘ ‘{print $4}‘ fstab
[[email protected] test]# awk -v FS=‘ ‘ -v OFS=‘:‘ ‘{print $3,$4}‘ fstab #OFS 相当于把$3,$4 替换成$3 【OFS】$4
by:anaconda
#RS:input record seperator, 输入 默认为行分割符;
#ORS:output record seperator, 输出 默认为行分割符; (这两个一般很少用,文本从本来一行一行,换成其他格式)
#-----------------------------------------------------------------
[[email protected] test]# awk -v RS=‘\n‘ -v ORS=‘\n‘ ‘{print $1}‘ fstab #默认
[[email protected] test]# awk -v RS=‘ ‘ -v ORS=‘:‘ ‘{print "hello ",$2}‘ fstab #
hello #:hello #:hello ......
#NF number of field 每行字段数目 $#
#-----------------------------------------------------------------
[[email protected] test]# awk ‘{print NF}‘ fstab
#最后一个字段值
#-----------------------------------------------------------------
[[email protected] test]# awk ‘{print $NF}‘ fstab
#NR:number of record, 行号;
#-----------------------------------------------------------------
[[email protected] test]# awk ‘{print NR}‘ fstab
#FNR:各文件分别计数;行数;
#-----------------------------------------------------------------
[[email protected] test]# awk ‘{print FNR}‘ fstab fstab
#自定义变量 -v var=value 变量名区分字符大小写;
#-----------------------------------------------------------------
[[email protected] test]# awk -v test=‘hello world‘ ‘{print test}‘ fstab
hello world
[[email protected] test]# awk ‘{test="hello world"; print test}‘ fstab
hello world
3、printf命令 用来显示样式
(1) FORMAT必须给出;
(2) 不会自动换行,需要显式给出换行控制符,\n
(3) FORMAT中需要分别为后面的每个item指定一个格式化符号;
格式符:
%c: 显示字符的ASCII码;
%d, %i: 显示十进制整数;
%e, %E: 科学计数法数值显示;
%f:显示为浮点数;
%g, %G:以科学计数法或浮点形式显示数值;
%s:显示字符串;
%u:无符号整数;
%%: 显示%自身;
修饰符:
#[.#]:第一个数字控制显示的宽度;第二个#表示小数点后的精度;
-: 左对齐
+:显示数值的符号
[[email protected] test]# awk ‘{printf "%50s\n",$1}‘ fstab
#
UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac
UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c
UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b
4、操作符
算术操作符:x+y, x-y, x*y, x/y, x^y, x%y -x +x: 转换为数值;字符串操作符:没有符号的操作符,字符串连接
赋值操作符:=, +=, -=, *=, /=, %=, ^= ++, --
比较操作符:>, >=, <, <=, !=, ==
模式匹配符: ~:是否匹配 !~:是否不匹配
逻辑操作符:&& || !
函数调用:function_name(argu1, argu2, ...)
条件表达式:selector?if-true-expression:if-false-expression
#-----------------------------------------------------------------
[[email protected] test]# awk -F: ‘{ $7~"/bash" ? is_bash="yes":is_bash="no";age=8+10;print $1,age,$7,is_bash}‘ passwd
root 18 /bin/bash yes
bin 18 /sbin/nologin no
5、定界
[[email protected] test]# awk ‘!/^UUID/{printf "%-50s\n",$1}‘ fstab #正则
[[email protected] test]# awk ‘/^UUID/{printf "%-50s\n",$1}‘ fstab
[[email protected] test]# awk ‘$3>1000{print $1,$3}‘ /etc/passwd #表达式
[[email protected] test]# awk -F: ‘$NF~"/bash"{print $1,$3}‘ /etc/passwd
[[email protected] test]# awk -F: ‘/^sync/,/^halt/{print $1,$3}‘ /etc/passwd
sync 5
shutdown 6
halt 7
[[email protected] test]# awk -F: ‘(NR>=6&&NR<=8){print NR,$1,$3}‘ /etc/passwd
[[email protected] test]# awk -F: ‘BEGIN{print "username uid \n---------"}{print $1,$3}‘ /etc/passwd
username uid
---------
root 0
bin 1
daemon 2
[[email protected] test]# awk -F: ‘{print "username uid \n---------";print $1,$3}‘ /etc/passwd
username uid
---------
root 0
username uid
---------
bin 1
username uid
6、控制语句
#if(condition) {statments}
[[email protected] test]# awk ‘{if(NF>5) print $0}‘ /etc/fstab
#if(condition) {statments} else {statements}
[[email protected] test]# awk -F: ‘{if($3>=1000) printf "Common user: %s\n",$1 ;else printf "root or Sysuser: %s\n",$1}‘ /etc/passwd
#-----------------------------------------------------------------
#while(conditon) {statments}
[[email protected] test]# awk ‘/^UUID/{i=1;while(i<=NF) {print $i;i++}}‘ fstab
#do statement while(condition) 先执行在判断
[[email protected] test]# awk ‘/^UUID/{i=1;do {print $i;i++;} while(i<=NF)}‘ fstab
#-----------------------------------------------------------------
#for(expr1;expr2;expr3) statement
[[email protected] test]# awk ‘{for(i=0;i<NF;i++){print $i}}‘ fstab
[[email protected] test]# awk ‘BEGIN{names["a"]="zander"; names["b"]="marvin";for(i in names){print i,names[i] }}‘ fstab #只能是关联数组 names[0]="zander"; 这个也是关联数组
a zander
b marvin
#-----------------------------------------------------------------
#switch(expression) {case VALUE1 or /REGEXP/: statement; case VALUE2 or /REGEXP2/: statement; ...; default: statement}
#next 提前结束对本行的处理而直接进入下一行;
#-----------------------------------------------------------------
[[email protected] test]# awk -F: ‘{if($3%2!=0) next; print $1,$3}‘ /etc/passwd
7、数组操作
关联数组:array[index-expression]
#index in array
[[email protected] test]# awk ‘BEGIN{weekdays["mon"]="Monday";weekdays["tue"]="Tuesday"; if("mon" in weekdays){print weekdays["mon"]}else{print "not exist"}}‘
Monday
#for(var in array) {for-body}
[[email protected] test]# awk ‘BEGIN{weekdays["mon"]="Monday";weekdays["tue"]="Tuesday";for(i in weekdays) {print weekdays[i]}}‘
统计 去重复
[[email protected] test]# netstat -tan | awk ‘/^tcp\>/{state[$NF]++}END{for(i in state) { print i,state[i]}}‘
LISTEN 2
ESTABLISHED 3
8、函数
#rand():返回0和1之间一个随机数;
#-----------------------------------------------------------------
[[email protected] test]# awk ‘BEGIN{print rand()}‘
0.237788
#length([s]):返回指定字符串的长度;
#-----------------------------------------------------------------
[[email protected] test]# awk ‘BEGIN{name="zander"; print length(name)}‘
6
#sub 替换 -----> 一次
#-----------------------------------------------------------------
[[email protected] test]# awk ‘BEGIN{name="zander"; print sub("a","A",name),name}‘
1 zAnder
#sub 替换 -----> 全部
#-----------------------------------------------------------------
[[email protected] test]# awk ‘BEGIN{name="zandera"; print gsub("a","A",name),name}‘
2 zAnderA
#split(s,a[,r]):以r为分隔符切割字符s,并将切割后的结果保存至a所表示的数组中;
#------------------------------------------------------------------
[[email protected] test]# netstat -tan|awk ‘/^tcp\>/{split($5,ip,":"); count[ip[1]]+=1;}END{for(i in count){print i,count[i]}}‘
以上是关于Linux 文本处理工具awk的主要内容,如果未能解决你的问题,请参考以下文章