<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Shell文本處理三劍客:grep、sed、awk

          共 14908字,需瀏覽 30分鐘

           ·

          2020-08-19 03:40

          來(lái)源:https://blog.csdn.net/Z_Date/article/details/107829161

          grep、sed、awk我們叫他們?nèi)齽停莆账鼈兛梢愿玫倪\(yùn)維,提升工作效率,即使不是運(yùn)維,對(duì)我們處理數(shù)據(jù)都是非常方便的~就很多數(shù)據(jù)處理來(lái)講,寫程序肯定是也能處理的,但是遠(yuǎn)沒(méi)有已經(jīng)存在特定功能的命令更高效,我們只需要操作命令即可。通過(guò)本文可以講解三劍客的一些基礎(chǔ)知識(shí)和實(shí)用,希望大家可以自己動(dòng)手敲,畢竟自己體會(huì)過(guò)的印象更深刻,后面還會(huì)持續(xù)更新。。。

          grep

          簡(jiǎn)介

          grep是一款強(qiáng)大的文本搜索工具,支持正則表達(dá)式。

          全稱( global search regular expression(RE) and print out the line)

          語(yǔ)法:grep [option]... PATTERN [FILE]...

          常用:

          usage:?grep?[-abcDEFGHhIiJLlmnOoqRSsUVvwxZ]?[-A?num]?[-B?num]?[-C[num]]
          ?[-e?pattern]?[-f?file]?[--binary-files=value]?[--color=when]
          ?[--context[=num]]?[--directories=action]?[--label]?[--line-buffered]
          ?[--null]?[pattern]?[file?...]

          常用參數(shù):

          ????????????-v????????取反
          ????????????-i????????忽略大小寫
          ????????????-c????????符合條件的行數(shù)
          ????????????-n????????輸出的同時(shí)打印行號(hào)
          ????????????^*????????以*開頭?????????
          ????????????*$?????????以*結(jié)尾?
          ????????????^$?????????空行?

          實(shí)際使用

          準(zhǔn)備好一個(gè)小故事txt:

          [root@iz2ze76ybn73dvwmdij06zz?~]#?cat?monkey
          One?day,a?little?monkey?is?playing?by?the?well.一天,有只小猴子在井邊玩兒.
          He?looks?in?the well and shouts :它往井里一瞧,高喊道:
          “Oh!My?god!The?moon?has?fallen?into?the?well!”?“噢!我的天!月亮掉到井里頭啦!”
          An?older?monkeys?runs?over,takes?a?look,and?says,一只大猴子跑來(lái)一看,說(shuō),
          “Goodness?me!The?moon?is?really?in?the?water!”?“糟啦!月亮掉在井里頭啦!”
          And?olderly?monkey?comes?over.老猴子也跑過(guò)來(lái).
          He is very surprised as well and cries out:他也非常驚奇,喊道:
          “The?moon?is?in?the?well.”?“糟了,月亮掉在井里頭了!”
          A?group?of?monkeys?run?over?to?the?well?.一群猴子跑到井邊來(lái),
          They?look?at?the?moon?in?the well and shout:他們看到井里的月亮,喊道:
          “The?moon?did?fall?into?the?well!Come?on!Let’get?it?out!”
          “月亮掉在井里頭啦!快來(lái)!讓我們把它撈起來(lái)!”
          Then,the?oldest?monkey?hangs?on?the?tree?up?side?down?,with?his?feet?on?the?branch?.
          然后,老猴子倒掛在大樹上,
          And?he?pulls?the?next?monkey’s?feet?with?his?hands.拉住大猴子的腳,
          All?the?other?monkeys?follow?his?suit,其他的猴子一個(gè)個(gè)跟著,
          And?they?join?each?other?one?by?one?down?to?the?moon?in?the?well.
          它們一只連著一只直到井里.
          Just?before?they?reach?the?moon,the?oldest?monkey?raises?his?head?and?happens?to?see?the?moon?in?the?sky,正好他們摸到月亮的時(shí)候,老猴子抬頭發(fā)現(xiàn)月亮掛在天上呢
          He?yells?excitedly?“Don’t?be?so?foolish!The?moon?is?still?in?the?sky!”
          它興奮地大叫:“別蠢了!月亮還好好地掛在天上呢!
          直接查找符合條件的行
          [root@iz2ze76ybn73dvwmdij06zz?~]#?grep?moon?monkey
          “Oh!My?god!The?moon?has?fallen?into?the?well!”?“噢!我的天!月亮掉到井里頭啦!”
          “Goodness?me!The?moon?is?really?in?the?water!”?“糟啦!月亮掉在井里頭啦!”
          “The?moon?is?in?the?well.”?“糟了,月亮掉在井里頭了!”
          They?look?at?the?moon?in?the well and shout:他們看到井里的月亮,喊道:
          “The?moon?did?fall?into?the?well!Come?on!Let’get?it?out!”
          And?they?join?each?other?one?by?one?down?to?the?moon?in?the?well.
          Just?before?they?reach?the?moon,the?oldest?monkey?raises?his?head?and?happens?to?see?the?moon?in?the?sky,正好他們摸到月亮的時(shí)候,老猴子抬頭發(fā)現(xiàn)月亮掛在天上呢
          He?yells?excitedly?“Don’t?be?so?foolish!The?moon?is?still?in?the?sky!”
          查找反向符合條件的行
          [root@iz2ze76ybn73dvwmdij06zz?~]#?grep?-v??moon?monkey
          One?day,a?little?monkey?is?playing?by?the?well.一天,有只小猴子在井邊玩兒.
          He?looks?in?the well and shouts :它往井里一瞧,高喊道:
          An?older?monkeys?runs?over,takes?a?look,and?says,一只大猴子跑來(lái)一看,說(shuō),
          And?olderly?monkey?comes?over.老猴子也跑過(guò)來(lái).
          He is very surprised as well and cries out:他也非常驚奇,喊道:
          A?group?of?monkeys?run?over?to?the?well?.一群猴子跑到井邊來(lái),
          “月亮掉在井里頭啦!快來(lái)!讓我們把它撈起來(lái)!”
          Then,the?oldest?monkey?hangs?on?the?tree?up?side?down?,with?his?feet?on?the?branch?.
          然后,老猴子倒掛在大樹上,
          And?he?pulls?the?next?monkey’s?feet?with?his?hands.拉住大猴子的腳,
          All?the?other?monkeys?follow?his?suit,其他的猴子一個(gè)個(gè)跟著,
          它們一只連著一只直到井里.
          它興奮地大叫:“別蠢了!月亮還好好地掛在天上呢!”
          直接查找符合條件的行數(shù)
          [root@iz2ze76ybn73dvwmdij06zz?~]#?grep?-c??moon?monkey
          8
          忽略大小寫查找符合條件的行數(shù)

          先來(lái)看一下直接查找的結(jié)果

          [root@iz2ze76ybn73dvwmdij06zz?~]#?grep?my?monkey

          忽略大小寫查看

          [root@iz2ze76ybn73dvwmdij06zz?~]#?grep?-i?my?monkey
          “Oh!My?god!The?moon?has?fallen?into?the?well!”?“噢!我的天!月亮掉到井里頭啦!”

          查找符合條件的行并輸出行號(hào)
          [root@iz2ze76ybn73dvwmdij06zz?~]#?grep?-n?monkey?monkey
          1:One?day,a?little?monkey?is?playing?by?the?well.一天,有只小猴子在井邊玩兒.
          4:An?older?monkeys?runs?over,takes?a?look,and?says,一只大猴子跑來(lái)一看,說(shuō),
          6:And?olderly?monkey?comes?over.老猴子也跑過(guò)來(lái).
          9:A?group?of?monkeys?run?over?to?the?well?.一群猴子跑到井邊來(lái),
          13:Then,the?oldest?monkey?hangs?on?the?tree?up?side?down?,with?his?feet?on?the?branch?.
          15:And?he?pulls?the?next?monkey’s?feet?with?his?hands.拉住大猴子的腳,
          16:All?the?other?monkeys?follow?his?suit,其他的猴子一個(gè)個(gè)跟著,
          19:Just?before?they?reach?the?moon,the?oldest?monkey?raises?his?head?and?happens?to?see?the?moon?in?the?sky,正好他們摸到月亮的時(shí)候,老猴子抬頭發(fā)現(xiàn)月亮掛在天上呢
          查找開頭是J的行
          [root@iz2ze76ybn73dvwmdij06zz?~]#?grep?'^J'?monkey
          Just?before?they?reach?the?moon,the?oldest?monkey?raises?his?head?and?happens?to?see?the?moon?in?the?sky,正好他們摸到月亮的時(shí)候,老猴子抬頭發(fā)現(xiàn)月亮掛在天上呢
          查找結(jié)尾是呢的行
          [root@iz2ze76ybn73dvwmdij06zz?~]#?grep?"呢$"?monkey
          Just?before?they?reach?the?moon,the?oldest?monkey?raises?his?head?and?happens?to?see?the?moon?in?the?sky,正好他們摸到月亮的時(shí)候,老猴子抬頭發(fā)現(xiàn)月亮掛在天上呢

          大家可以grep --help,查看更多相關(guān)的命令,這里就不一一演示了。

          小結(jié)

          有了強(qiáng)大的網(wǎng)絡(luò)以后,很多東西都可以在網(wǎng)上找到,但是基礎(chǔ)的一定要自己 熟練掌握,才回在遇到事情的時(shí)候不慌。

          sed

          sed是一種流編輯器,是一款處理文本比較優(yōu)秀的工具,可以結(jié)合正則表達(dá)式一起使用。

          sed執(zhí)行過(guò)程

          sed命令

          命令: sed

          語(yǔ)法 : ?sed [選項(xiàng)]... {命令集} [輸入文件]...

          常用命令:

          ????????????d??刪除選擇的行????
          ????????????s???查找????
          ????????????y??替換
          ????????????i???當(dāng)前行前面插入一行
          ????????????a??當(dāng)前行后面插入一行
          ????????????p??打印行???????
          ????????????q??退出?????

          ?替換符:

          ????????????數(shù)字?:替換第幾處????
          ????????????g?:??全局替換????
          ????????????\1:??子串匹配標(biāo)記,前面搜索可以用元字符集\(..\)
          ????????????&:??保留搜索刀的字符用來(lái)替換其他字符

          操作:

          替換

          查看文件:

          ???happy?cat?word
          Twinkle,?twinkle,?little?star
          How?I?wonder?what?you?are
          Up?above?the?world?so?high
          Like?a?diamond?in?the?sky
          When?the?blazing?sun?is?gone

          替換:

          ???happy?sed?'s/little/big/'?word
          Twinkle,?twinkle,?big?star
          How?I?wonder?what?you?are
          Up?above?the?world?so?high
          Like?a?diamond?in?the?sky
          When?the?blazing?sun?is?gone

          查看文本:

          ???happy?cat?word1
          Oh?if?there's?one?thing?to?be?taught
          it'
          s?dreams?are?made?to?be?caught
          and?friends?can?never?be?bought
          Doesn't?matter?how?long?it's?been
          I?know?you'll?always?jump?in
          '
          Cause?we?don't?know?how?to?quit

          全局替換:

          ???happy?sed?'s/to/can/g'?word1
          Oh?if?there's?one?thing?can?be?taught
          it'
          s?dreams?are?made?can?be?caught
          and?friends?can?never?be?bought
          Doesn't?matter?how?long?it's?been
          I?know?you'll?always?jump?in
          '
          Cause?we?don't?know?how?can?quit

          按行替換(替換2到最后一行)

          ???happy?sed?'2,$s/to/can/'?word1
          Oh?if?there's?one?thing?to?be?taught
          it'
          s?dreams?are?made?can?be?caught
          and?friends?can?never?be?bought
          Doesn't?matter?how?long?it's?been
          I?know?you'll?always?jump?in
          '
          Cause?we?don't?know?how?can?quit
          刪除:

          查看文本:

          ???happy?cat?word
          Twinkle,?twinkle,?little?star
          How?I?wonder?what?you?are
          Up?above?the?world?so?high
          Like?a?diamond?in?the?sky
          When?the?blazing?sun?is?gone

          刪除:

          ???happy?sed?'2d'?word
          Twinkle,?twinkle,?little?star
          Up?above?the?world?so?high
          Like?a?diamond?in?the?sky
          When?the?blazing?sun?is?gone

          顯示行號(hào):

          ???happy?sed?'=;2d'?word
          1
          Twinkle,?twinkle,?little?star
          2
          3
          Up?above?the?world?so?high
          4
          Like?a?diamond?in?the?sky
          5
          When?the?blazing?sun?is?gone

          刪除第2行到第四行:

          ???happy?sed?'=;2,4d'?word
          1
          Twinkle,?twinkle,?little?star
          2
          3
          4
          5
          When?the?blazing?sun?is?gone
          添加行:

          向前插入:

          ???happy?echo?"hello"?|?sed?'i\kitty'
          kitty
          hello

          向后插入:

          ???happy?echo?"kitty"?|?sed?'i\hello'
          hello
          kitty
          修改行:

          替換第二行為hello kitty

          ???happy?sed?'2c\hello?kitty'?word
          Twinkle,?twinkle,?little?star
          hello?kitty
          Up?above?the?world?so?high
          Like?a?diamond?in?the?sky
          When?the?blazing?sun?is?gone

          替換第二行到最后一行為hello kitty

          ???happy?sed?'2,$c\hello?kitty'?word
          Twinkle,?twinkle,?little?star
          hello?kitty
          寫入行

          把帶star的行寫入c文件中,c提前創(chuàng)建

          ???happy?sed?-n?'/star/w?c'?word
          ???happy?cat?c
          Twinkle,?twinkle,?little?star
          退出

          打印3行后,退出sed

          ???happy?sed?'3q'?word
          Twinkle,?twinkle,?little?star
          How?I?wonder?what?you?are
          Up?above?the?world?so?high

          awk

          名字由來(lái)

          創(chuàng)始人 Alfred Aho 、Peter Weinberger 和 Brian Kernighan 姓氏的首個(gè)字母。

          強(qiáng)大的文本處理工具

          比起sed和grep,awk不僅僅是一個(gè)小工具,也可以算得上一種小型的編程語(yǔ)言了,支持if判斷分支和while循環(huán)語(yǔ)句還有它的內(nèi)置函數(shù)等,是一個(gè)要比grep和sed更強(qiáng)大的文本處理工具,但也就意味著要學(xué)習(xí)的東西更多了。

          下面來(lái)說(shuō)一下awk的一些基礎(chǔ)概念以及實(shí)際操作。

          語(yǔ)法

          常用

          Usage: awk [POSIX or GNU style options] -f progfile [--] file ...

          Usage: awk [POSIX or GNU style options] [--] 'program' file ...

          類似數(shù)據(jù)庫(kù)列的概念,但它是按照序號(hào)來(lái)指定的,比如我要第一個(gè)列就是2,依此類推。$0就是輸出整個(gè)文本的內(nèi)容。默認(rèn)用空格作為分隔符,當(dāng)然你可以自己通過(guò)-F設(shè)置適合自己情況的分隔符。

          提前自己編了一段數(shù)據(jù),學(xué)生以及學(xué)生成績(jī)數(shù)據(jù)表。

          列數(shù)名稱描述
          1Name姓名
          2Math數(shù)學(xué)
          3Chinese語(yǔ)文
          4English英語(yǔ)
          5History歷史
          6Sport體育
          8Grade班級(jí)

          "Name ? ? Math ?Chinese ?English History ?Sport grade 輸出整個(gè)文本

          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'{print?$0}'?students_store
          Xiaoka??????????60???80????40????90???77??class-1
          Yizhihua????????70????66???50????80???90??class-1
          kerwin??????????80????90???60????70???60??class-2
          Fengzheng???????90????78????62???40???62??class-2

          輸出第一列(姓名列)

          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'{print?$1}'?students_store
          Xiaoka
          Yizhihua
          kerwin
          Fengzheng

          模式&動(dòng)作

          awk?'{[pattern]?action}'?{filenames}??

          模式

          pattern 可以是

          • 條件語(yǔ)句
          • 正則

          模式的兩個(gè)特殊字段 BEGIN 和 END (不指定時(shí)匹配或打印行數(shù))

          • BEGIN :一般用來(lái)打印列名稱。

          • END : 一般用來(lái)打印總結(jié)性質(zhì)的字符。

          動(dòng)作

          action 在{}內(nèi)指定,一般用來(lái)打印,也可以是一個(gè)代碼段。

          示例

          給上面的文本加入標(biāo)題頭:

          [root@iz2ze76ybn73dvwmdij06zz?~]#??awk?'BEGIN?{print?"Name?????Math??Chinese??English?History??Sport?grade\n----------------------------------------------"}?{print?$0}'?students_store

          Name?????????Math??Chinese??English?History??Sport??grade
          ----------------------------------------------------------
          Xiaoka???????60????80???????40??????90?????77????class-1
          Yizhihua?????70????66???????50??????80?????90????class-1
          kerwin???????80????90???????60??????70?????60????class-2
          Fengzheng????90????78???????62??????40?????62????class-2

          僅打印姓名、數(shù)學(xué)成績(jī)、班級(jí)信息,再加一個(gè)文尾(再接再厲):

          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'BEGIN?{print?"Name???Math??grade\n---------------------"}?{print?$1?2?"\t"?$7}?END?{print?"continue?to?exert?oneself"}'?students_store

          Name?????Math??grade
          ---------------------
          Xiaoka???60???class-1
          Yizhihua?70???class-1
          kerwin???80???class-2
          Fengzheng?90??class-2
          continue?to?exert?oneself

          結(jié)合正則

          像grep和sed也是支持正則表達(dá)式的。這邊就不介紹正則表達(dá)式了,如果有興趣,我單出一個(gè)文章。

          使用方法:

          符號(hào) ~ ?后接正則表達(dá)式

          此時(shí)我們?cè)偌尤胍粭l后來(lái)的新同學(xué),并且沒(méi)有分班。

          先來(lái)看下現(xiàn)在的數(shù)據(jù)

          [root@iz2ze76ybn73dvwmdij06zz?~]#?cat?students_store
          Xiaoka???????60???80????40????90???77??class-1
          Yizhihua?????70????66???50????80???90??class-1
          kerwin???????80????90???60????70???60??class-2
          Fengzheng????90????78???62????40???62??class-2
          xman?????????-?????-?????-?????-???-????-

          模糊匹配|查詢已經(jīng)分班的學(xué)生

          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'$0?~/class/'?students_store
          Xiaoka???????60???80????40????90???77??class-1
          Yizhihua?????70????66???50????80???90??class-1
          kerwin???????80????90???60?????70??60??class-2
          Fengzheng????90????78???62?????40??62??class-2

          精準(zhǔn)匹配|查詢1班的學(xué)生

          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'$7=="class-1"?{print?$0}'??students_store
          Xiaoka???????60???80????40????90???77??class-1
          Yizhihua?????70????66???50????80???90??class-1

          反向匹配|查詢不是1班的學(xué)生

          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'$7!="class-1"?{print?$0}'??students_store
          kerwin???????80????90???60?????70???60??class-2
          Fengzheng????90????78????62?????40??62?class-2
          xman?????????-?????-?????-?????-???-????-

          比較操作

          查詢數(shù)學(xué)大于80的

          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'$2>60?{print?$0}'??students_store
          Yizhihua?????70????66???50????80???90??class-1
          kerwin???????80????90???60?????70???60??class-2
          Fengzheng????90????78????62?????40??62?class-2

          查詢數(shù)學(xué)大于英語(yǔ)成績(jī)的

          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'$2?>?$4??{print?$0}'??students_store
          Xiaoka???????60???80????40????90???77??class-1
          Yizhihua?????70????66???50????80???90??class-1
          kerwin???????80????90???60????70???60??class-2
          Fengzheng????90????78????62???40???62??class-2

          匹配指定字符中的任意字符

          在加一列專業(yè),讓我們來(lái)看看憨憨們的專業(yè),順便給最后一個(gè)新來(lái)的同學(xué)分個(gè)班吧。

          然后再來(lái)看下此時(shí)的數(shù)據(jù)。

          [root@iz2ze76ybn73dvwmdij06zz?~]#?cat?students_store
          Xiaoka???????60???80????40????90???77??class-1??Java
          Yizhihua?????70????66???50????80???90??class-1??java
          kerwin???????80????90???60?????70???60??class-2?Java
          Fengzheng????90????78????62?????40??62?class-2??java
          xman?????????-?????-?????-?????-???-????class-3?php

          或關(guān)系匹配|查詢1班和3班的學(xué)生

          root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'$0?~/(class-1|class-3)/'?students_store
          Xiaoka???????60???80????40????90???77??class-1??Java
          Yizhihua?????70????66???50????80???90??class-1??java
          xman?????????-?????-?????-?????-???-???class-3?php

          任意字符匹配|名字第二個(gè)字母是

          字符解釋:

          ^ : 字段或記錄的開頭。

          . : 任意字符。

          root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'$0?~/(class-1|class-3)/'?students_store
          Xiaoka???????60???80????40????90???77??class-1??Java
          Yizhihua?????70????66???50????80???90??class-1??java
          xman?????????-?????-?????-?????-???-????class-3?php

          復(fù)合表達(dá)式

          && AND

          的關(guān)系,必同時(shí)滿足才行哦~

          查詢數(shù)學(xué)成績(jī)大于60并且語(yǔ)文成績(jī)也大于60的童鞋。

          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'{?if?($2?>?60?&&?$3?>?60)?print?$0}'?students_store
          Yizhihua?????70????66???50????80???90??class-1??java
          kerwin???????80????90???60????70???60??class-2??Java
          Fengzheng????90????78????62???40???62??class-2??java

          || ?OR

          查詢數(shù)學(xué)大于80或者語(yǔ)文大于80的童鞋。

          [root@iz2ze76ybn73dvwmdij06zz?~]#??awk?'{?if?($2?>?80?||?$4?>?80)?print?$0}'?students_store
          Fengzheng????90????78????62?????40??62?class-2??java

          printf 格式化輸出

          除了能達(dá)到功能以外,一個(gè)好看的格式也是必不可少的,因此格式化的輸出看起來(lái)會(huì)更舒服哦~

          語(yǔ)法

          printf ([格式],參數(shù))

          printf %x(格式) 具體參數(shù) x代表具體格式

          符號(hào)說(shuō)明
          -左對(duì)齊
          Width域的步長(zhǎng)
          .prec最大字符串長(zhǎng)度或小數(shù)點(diǎn)右邊位數(shù)

          格式轉(zhuǎn)化符

          其實(shí)和其他語(yǔ)言大同小異的

          常用格式

          符號(hào)描述
          %cASCII
          %d整數(shù)
          %o八進(jìn)制
          %x十六進(jìn)制數(shù)
          %f浮點(diǎn)數(shù)
          %e浮點(diǎn)數(shù)(科學(xué)記數(shù)法)
          % s字符串
          %g決定使用浮點(diǎn)轉(zhuǎn)化e/f

          具體操作示例

          ASCII碼?

          [root@iz2ze76ybn73dvwmdij06zz?~]#?echo?"66"?|?awk?'{printf?"%c\n",$0}'
          B

          浮點(diǎn)數(shù)

          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'BEGIN?{printf?"%f\n",100}'
          100.000000

          16進(jìn)制數(shù)

          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'BEGIN?{printf?"%x",996}'
          3e4

          更多操作,小伙伴有興趣可以挨個(gè)試試~

          內(nèi)置變量

          頻率較高常用內(nèi)置變量

          NF :記錄瀏覽域的個(gè)數(shù),在記錄被讀后設(shè)置。

          NR :已讀的記錄數(shù)。

          FS : 設(shè)置輸入域分隔符

          A R G C :命令行參數(shù)個(gè)數(shù),支持命令行傳入。

          RS : 控制記錄分隔符

          FIlENAME : awk當(dāng)前讀文件的名稱

          操作

          輸出學(xué)生成績(jī)表和域個(gè)數(shù)以及已讀記錄數(shù)。

          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'{print?$0,?NF?,?NR}'?students_store
          Xiaoka???????60???80????40????90???77??class-1??Java?8?1
          Yizhihua?????70????66???50????80???90??class-1??java?8?2
          kerwin???????80????90???60?????70??60??class-2??Java?8?3
          Fengzheng????90????78???62?????40??62??class-2??java?8?4
          xman?????????-?????-?????-?????-???-???class-3??php??8?5

          內(nèi)置函數(shù)

          常用函數(shù)

          length(s) ?返回s長(zhǎng)度

          index(s,t) 返回s中字符串t第一次出現(xiàn)的位置

          match (s,r) s中是否包含r字符串

          split(s,a,fs) 在fs上將s分成序列a

          gsub(r,s) 用s代替r,范圍全文本

          gsub(r,s,t) 范圍t中,s代替r

          substr(s,p) 返回字符串s從第p個(gè)位置開始后面的部分(下標(biāo)是從1 開始算的,大家可以自己試試)

          substr(s,p,n) 返回字符串s從第p個(gè)位置開始后面n個(gè)字符串的部分

          操作

          length
          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'BEGIN?{print?length("?hello,im?xiaoka")}'
          16
          index
          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'BEGIN?{print?index("xiaoka","ok")}'
          4
          match
          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'BEGIN?{print?match("Java小咖秀","va小")}'
          3
          gsub
          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'gsub("Xiaoka","xk")?{print?$0}'?students_store
          xk???????60???80????40????90???77??class-1??Java
          substr(s,p)
          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'BEGIN?{print?substr("xiaoka",3)}'
          aoka
          substr(s,p,n)
          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'BEGIN?{print?substr("xiaoka",3,2)}'
          ao
          split
          [root@iz2ze76ybn73dvwmdij06zz?~]#?str="java,xiao,ka,xiu"
          [root@iz2ze76ybn73dvwmdij06zz?~]#?awk?'BEGIN{split('"\"$str\""',ary,",");?for(i?in?ary)?{if(ary[i]>1)?print?ary[i]}}'
          xiu
          java
          xiao
          ka

          awk腳本

          前面說(shuō)過(guò)awk是可以說(shuō)是一個(gè)小型編程語(yǔ)言。如果命令比較短我們可以直接在命令行執(zhí)行,當(dāng)命令行比較長(zhǎng)的時(shí)候,可以使用腳本來(lái)處理,比命令行的可讀性更高,還可以加上注釋。

          寫一個(gè)完整的awk腳本并執(zhí)行步驟

          1.先創(chuàng)建一個(gè)awk文件
          [root@iz2ze76ybn73dvwmdij06zz?~]#?vim?printname.awk
          2.腳本第一行要指定解釋器
          #!/usr/bin/awk?-f
          3.編寫腳本內(nèi)容,打印一下名稱
          [root@iz2ze76ybn73dvwmdij06zz?~]#?cat?printname.awk
          #!/usr/bin/awk?-f
          #可以加注釋了,哈哈
          BEGIN?{?print?"my?name?is?Java小咖秀"}
          4.既然是腳本,必不可少的可執(zhí)行權(quán)限安排上~
          [root@iz2ze76ybn73dvwmdij06zz?~]#?chmod?+x?printname.awk
          [root@iz2ze76ybn73dvwmdij06zz?~]#?ll?printname.awk
          -rwxr-xr-x?1?root?root?60?7月???1?15:23?printname.awk
          5.有了可執(zhí)行權(quán)限,我們來(lái)執(zhí)行下看結(jié)果
          [root@iz2ze76ybn73dvwmdij06zz?~]#?./printname.awk
          my?name?is?Java小咖秀

          了解了寫awk腳本的步驟以后大家就可以自己去寫一波了~


          - END -

          精彩文章推薦:

          Java 應(yīng)用運(yùn)維最常見的3個(gè)問(wèn)題排查思路

          SSH只能用于遠(yuǎn)程Linux主機(jī)?那說(shuō)明你見識(shí)太短了!

          10 個(gè)Linux Awk文本處理經(jīng)典案例

          Nginx實(shí)現(xiàn)高效負(fù)載均衡器就是這么簡(jiǎn)單!

          面試官:Redis監(jiān)控指標(biāo)有哪些?

          10 分鐘部署一個(gè) Kubernetes 集群

          部署一套完整的Kubernetes高可用集群(二進(jìn)制)

          從網(wǎng)管到架構(gòu)師再到微創(chuàng)業(yè),我這9年的成長(zhǎng)感悟




          點(diǎn)亮,服務(wù)器三年不宕機(jī)

          瀏覽 44
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  韩国三级小视频 | 91色色色| av爱久| 逼特逼视频在线免费观看 | 韩国一区不卡 |