Home php教程 PHP开发 Detailed introduction and examples of awk command and awk programming language

Detailed introduction and examples of awk command and awk programming language

Dec 15, 2016 am 10:29 AM

一,什么是awk

awk是linux下的一个命令,他对其他命令的输出,对文件的处理都十分强大,其实他更像一门编程语言,他可以自定义变量,有条件语句,有循环,有数组,有正则,有函数等。他读取输出,或者文件的方式是一行,一行的读,根据你给出的条件进行查找,并在找出来的行中进行操作,感觉他的设计思想,真的很简单,但是结合实际情况,具体操作起来就没有那么简单了。他有三种形势,awk,gawk,nawk,平时所说的awk其实就是gawk。

二,awk中的记录,域,分割符

当我们读取输出时,或者读取文件时,读取一行就是一个记录。记录分割符是默认是回车符,保存在RS,ORS中。
我们从记录中分割出我们要单词,或者是词组等,我们称他为域,域分割符,默认的是空格和TAB銉,保存在内建变
量ORS中。举个例子:
aaaa:bbbb:ccccccc
1111:2343:5t43343
上面有二行,这二行就是二个记录,每行后面的回车呢,就是记录分割符,里面冒号呢,就是域分割符,分割出来的,aaaa,1111这类东西就是域了。
awk -F: '{print $1}' testfile

三,awk的内建变量和运算符

1,变量

Detailed introduction and examples of awk command and awk programming language

2,运算符

Detailed introduction and examples of awk command and awk programming language

四,awk的正则

Detailed introduction and examples of awk command and awk programming language

五,awk的函数

1,字符串函数

Detailed introduction and examples of awk command and awk programming language

2,数学函数

Detailed introduction and examples of awk command and awk programming language

六,实例

学习awk的时候,做了一个例子,学东西,不要光看,光看是记不住东西的。光看的话,也许你知道怎么回事,真正实际操作,不是这儿有问题,就是那儿有问题。所以一定要动手亲自操作一下。

1,测试文件test

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false
daemon:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
@zhangying:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po
Copy after login

例1:

cat test | awk -F: '{\
 if ($1 == "root"){\
 print $1;\
 }else if($1 == "bin"){\
 print $2;\
 }else{\
 print $3;\
 } \
}'
Copy after login

例2:

awk '{\
 for(i=0;i<NF;i++){\
 if ($i ~/^root/){\
 print $i;\
 }else if($i ~/zhangy/){\
 print $i;continue;\
 }else if($i ~/mysql/){\
 print $i;next;\
 }else if($i ~/^test/){\
 print $i;break;\
 } \
 }\
}&#39; test
Copy after login

例3:

tail test | awk &#39;BEGIN{while(getline d){ split(d,test);for(i in test){\
 print test[i]\
}}}&#39;
Copy after login

例4:

ls -al /home/zhangy/mytest | awk &#39;BEGIN{while(getline d){ split(d,test);\
 print test[9] ;}
}&#39;
Copy after login

例5:

echo "32:34" |awk -F: &#39;{print "max = ",max($1,$2)}\
function max(one,two){
if(one > two){
 return one;
}else{
 return two;
}
}
&#39;
Copy after login

例6:

#awk &#39;BEGIN{print "what is your name"; getline name < "/dev/tty"}$1 ~name{print
#"found name on line" NR}END{print "see you" name}&#39; test
#awk &#39;{sub(/daemon/,"tankzhang");print}&#39; test
#awk &#39;{{sub(/zhangy/,"tankzhang");$1};print}&#39; test
#awk &#39;{{gsub(/zhangy/,"tankzhang");$1};print}&#39; test
#awk -F: &#39;{print index("zhangy",$1)}&#39; test
#awk -F: &#39;{print substr($1,1,2)}&#39; test
awk -F: &#39;{mat=match($1,/^[a-zA-Z]+$/);print mat,RSTART,RLENGTH}&#39; test
Copy after login

例7:

cat test |awk -F: &#39;\
 NF != 7{\
printf("line %d,does not have 7 fields:%s\n",NR,$0)}\
$1 !~ /^[A-Za-z0-9]/{printf("line %d,non alpha and numeric user id:%s: %s\n",NR,$1,$0)}\
$2 == "*" {printf("lind %d,no password:%s\n",NR,$0)}&#39;
Copy after login


更多awk命令、awk编程语言详细介绍和实例相关文章请关注PHP中文网!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)