国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP開(kāi)發(fā) Detailed introduction and examples of awk command and awk programming language

Detailed introduction and examples of awk command and awk programming language

Dec 12, 2016 pm 04:22 PM
awk command

一,什么是awk

awk是linux下的一個(gè)命令,他對(duì)其他命令的輸出,對(duì)文件的處理都十分強(qiáng)大,其實(shí)他更像一門編程語(yǔ)言,他可以自定義Detailed introduction and examples of awk command and awk programming language,有條件語(yǔ)句,有循環(huán),有數(shù)組,有正則,有函數(shù)等。他讀取輸出,或者文件的方式是一行,一行的讀,根據(jù)你給出的條件進(jìn)行查找,并在找出來(lái)的行中進(jìn)行操作,感覺(jué)他的設(shè)計(jì)思想,真的很簡(jiǎn)單,但是結(jié)合實(shí)際情況,具體操作起來(lái)就沒(méi)有那么簡(jiǎn)單了。他有三種形勢(shì),awk,gawk,nawk,平時(shí)所說(shuō)的awk其實(shí)就是gawk。

二,awk中的記錄,域,分割符

當(dāng)我們讀取輸出時(shí),或者讀取文件時(shí),讀取一行就是一個(gè)記錄。記錄分割符是默認(rèn)是回車符,保存在RS,ORS中。
我們從記錄中分割出我們要單詞,或者是詞組等,我們稱他為域,域分割符,默認(rèn)的是空格和TAB銉,保存在內(nèi)建變
量ORS中。舉個(gè)例子:
aaaa:bbbb:ccccccc
1111:2343:5t43343
上面有二行,這二行就是二個(gè)記錄,每行后面的回車呢,就是記錄分割符,里面冒號(hào)呢,就是域分割符,分割出來(lái)的,aaaa,1111這類東西就是域了。
awk -F: '{print $1}' testfile

三,awk的內(nèi)建Detailed introduction and examples of awk command and awk programming language和Detailed introduction and examples of awk command and awk programming language

1,Detailed introduction and examples of awk command and awk programming language

Detailed introduction and examples of awk command and awk programming language

2,Detailed introduction and examples of awk command and awk programming language

Detailed introduction and examples of awk command and awk programming language

四,Detailed introduction and examples of awk command and awk programming language

Detailed introduction and examples of awk command and awk programming language

五,awk的函數(shù)

1,Detailed introduction and examples of awk command and awk programming language

Detailed introduction and examples of awk command and awk programming language

2,Detailed introduction and examples of awk command and awk programming language

Detailed introduction and examples of awk command and awk programming language

六,實(shí)例

學(xué)習(xí)awk的時(shí)候,做了一個(gè)例子,學(xué)東西,不要光看,光看是記不住東西的。光看的話,也許你知道怎么回事,真正實(shí)際操作,不是這兒有問(wèn)題,就是那兒有問(wèn)題。所以一定要?jiǎng)邮钟H自操作一下。

1,測(cè)試文件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

例1:

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

例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

例3:

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

例4:

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

例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;

例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

例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;


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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

PHP Tutorial
1502
276