grep、sed、awk咱們叫他們三劍客,掌握它們能夠更好的運維,提高工做效率,即便不是運維,對咱們處理數據都是很是方便的~就不少數據處理來說,寫程序確定是也能處理的,可是遠沒有已經存在特定功能的命令更高效,咱們只須要操做命令便可。經過本文能夠講解三劍客的一些基礎知識和實用,但願你們能夠本身動手敲,畢竟本身體會過的印象更深入,後面還會持續更新。。。php
grep是一款強大的文本搜索工具,支持正則表達式。java
全稱( global search regular expression(RE) and print out the line)web
語法:grep [option]... PATTERN [FILE]...正則表達式
經常使用:shell
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 ...] 複製代碼
經常使用參數:數據庫
-v 取反 -i 忽略大小寫 -c 符合條件的行數 -n 輸出的同時打印行號 ^* 以*開頭 *$ 以*結尾 ^$ 空行 複製代碼
準備好一個小故事txt:express
[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,一隻大猴子跑來一看,說, 「Goodness me!The moon is really in the water!」 「糟啦!月亮掉在井裏頭啦!」 And olderly monkey comes over.老猴子也跑過來. He is very surprised as well and cries out:他也很是驚奇,喊道: 「The moon is in the well.」 「糟了,月亮掉在井裏頭了!」 A group of monkeys run over to 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!」 「月亮掉在井裏頭啦!快來!讓咱們把它撈起來!」 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,其餘的猴子一個個跟着, 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,正好他們摸到月亮的時候,老猴子擡頭髮現月亮掛在天上呢 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,正好他們摸到月亮的時候,老猴子擡頭髮現月亮掛在天上呢 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,一隻大猴子跑來一看,說, And olderly monkey comes over.老猴子也跑過來. He is very surprised as well and cries out:他也很是驚奇,喊道: A group of monkeys run over to the well .一羣猴子跑到井邊來, 「月亮掉在井裏頭啦!快來!讓咱們把它撈起來!」 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,其餘的猴子一個個跟着, 它們一隻連着一隻直到井裏. 它興奮地大叫:「別蠢了!月亮還好好地掛在天上呢!」 複製代碼
[root@iz2ze76ybn73dvwmdij06zz ~]# grep -c moon monkey
8 複製代碼
先來看一下直接查找的結果編程
[root@iz2ze76ybn73dvwmdij06zz ~]# grep my monkey
複製代碼
忽略大小寫查看vim
[root@iz2ze76ybn73dvwmdij06zz ~]# grep -i my monkey
「Oh!My god!The moon has fallen into the well!」 「噢!個人天!月亮掉到井裏頭啦!」 複製代碼
[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,一隻大猴子跑來一看,說, 6:And olderly monkey comes over.老猴子也跑過來. 9:A group of monkeys run over to the well .一羣猴子跑到井邊來, 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,其餘的猴子一個個跟着, 19:Just before they reach the moon,the oldest monkey raises his head and happens to see the moon in the sky,正好他們摸到月亮的時候,老猴子擡頭髮現月亮掛在天上呢 複製代碼
[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,正好他們摸到月亮的時候,老猴子擡頭髮現月亮掛在天上呢 複製代碼
[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,正好他們摸到月亮的時候,老猴子擡頭髮現月亮掛在天上呢 複製代碼
你們能夠grep --help,查看更多相關的命令,這裏就不一一演示了。網絡
有了強大的網絡之後,不少東西均可以在網上找到,可是基礎的必定要本身 熟練掌握,纔回在遇到事情的時候不慌。
sed是一種流編輯器,是一款處理文本比較優秀的工具,能夠結合正則表達式一塊兒使用。
命令: sed
語法 : sed [選項]... {命令集} [輸入文件]...
經常使用命令:
d 刪除選擇的行 s 查找 y 替換 i 當前行前面插入一行 a 當前行後面插入一行 p 打印行 q 退出 替換符: 數字 :替換第幾處 g : 全局替換 \1: 子串匹配標記,前面搜索能夠用元字符集\(..\) &: 保留搜索刀的字符用來替換其餘字符 複製代碼
操做:
查看文件:
➜ 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 複製代碼
顯示行號:
➜ 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提早建立
➜ 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 複製代碼
創始人 Alfred Aho 、Peter Weinberger 和 Brian Kernighan 姓氏的首個字母。
比起sed和grep,awk不只僅是一個小工具,也能夠算得上一種小型的編程語言了,支持if判斷分支和while循環語句還有它的內置函數等,是一個要比grep和sed更強大的文本處理工具,但也就意味着要學習的東西更多了。
下面來講一下awk的一些基礎概念以及實際操做。
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
相似數據庫列的概念,但它是按照序號來指定的,好比我要第一個列就是2,依此類推。$0就是輸出整個文本的內容。默認用空格做爲分隔符,固然你能夠本身經過-F設置適合本身狀況的分隔符。
提早本身編了一段數據,學生以及學生成績數據表。
列數 | 名稱 | 描述 |
---|---|---|
1 | Name | 姓名 |
2 | Math | 數學 |
3 | Chinese | 語文 |
4 | English | 英語 |
5 | History | 歷史 |
6 | Sport | 體育 |
8 | Grade | 班級 |
"Name Math Chinese English History Sport grade 輸出整個文本
[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 複製代碼
awk '{[pattern] action}' {filenames}
複製代碼
pattern 能夠是
模式的兩個特殊字段 BEGIN 和 END (不指定時匹配或打印行數)
BEGIN : 通常用來打印列名稱。
END : 通常用來打印總結性質的字符。
action 在{}內指定,通常用來打印,也能夠是一個代碼段。
給上面的文本加入標題頭:
[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 複製代碼
僅打印姓名、數學成績、班級信息,再加一個文尾(再接再礪):
[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 複製代碼
像grep和sed也是支持正則表達式的。這邊就不介紹正則表達式了,若是有興趣,我單出一個文章。
使用方法:
符號 ~ 後接正則表達式
此時咱們再加入一條後來的新同窗,而且沒有分班。
先來看下如今的數據
[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 - - - - - - 複製代碼
[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 複製代碼
[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 複製代碼
[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 - - - - - - 複製代碼
[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 複製代碼
[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 複製代碼
在加一列專業,讓咱們來看看憨憨們的專業,順便給最後一個新來的同窗分個班吧。
而後再來看下此時的數據。
[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 複製代碼
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 複製代碼
字符解釋:
^ : 字段或記錄的開頭。
. : 任意字符。
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 複製代碼
的關係,必同時知足才行哦~
查詢數學成績大於60而且語文成績也大於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 複製代碼
查詢數學大於80或者語文大於80的童鞋。
[root@iz2ze76ybn73dvwmdij06zz ~]# awk '{ if ($2 > 80 || $4 > 80) print $0}' students_store
Fengzheng 90 78 62 40 62 class-2 java 複製代碼
除了能達到功能之外,一個好看的格式也是必不可少的,所以格式化的輸出看起來會更舒服哦~
printf ([格式],參數)
printf %x(格式) 具體參數 x表明具體格式
符號 | 說明 |
---|---|
- | 左對齊 |
Width | 域的步長 |
.prec | 最大字符串長度或小數點右邊位數 |
格式轉化符
其實和其餘語言大同小異的
經常使用格式
符號 | 描述 |
---|---|
%c | ASCII |
%d | 整數 |
%o | 八進制 |
%x | 十六進制數 |
%f | 浮點數 |
%e | 浮點數(科學記數法) |
% s | 字符串 |
%g | 決定使用浮點轉化e/f |
ASCII碼🐎
[root@iz2ze76ybn73dvwmdij06zz ~]# echo "66" | awk '{printf "%c\n",$0}'
B 複製代碼
浮點數
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN {printf "%f\n",100}'
100.000000 複製代碼
16進制數
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN {printf "%x",996}'
3e4 複製代碼
更多操做,小夥伴有興趣能夠挨個試試~
NF : 記錄瀏覽域的個數,在記錄被讀後設置。
NR : 已讀的記錄數。
FS : 設置輸入域分隔符
A R G C : 命令行參數個數,支持命令行傳入。
RS : 控制記錄分隔符
FIlENAME : awk當前讀文件的名稱
輸出學生成績表和域個數以及已讀記錄數。
[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 複製代碼
length(s) 返回s長度
index(s,t) 返回s中字符串t第一次出現的位置
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個位置開始後面的部分(下標是從1 開始算的,你們能夠本身試試)
substr(s,p,n) 返回字符串s從第p個位置開始後面n個字符串的部分
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN {print length(" hello,im xiaoka")}'
16 複製代碼
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN {print index("xiaoka","ok")}'
4 複製代碼
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN {print match("Java小咖秀","va小")}'
3 複製代碼
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'gsub("Xiaoka","xk") {print $0}' students_store
xk 60 80 40 90 77 class-1 Java 複製代碼
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN {print substr("xiaoka",3)}'
aoka 複製代碼
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN {print substr("xiaoka",3,2)}'
ao 複製代碼
[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是能夠說是一個小型編程語言。若是命令比較短咱們能夠直接在命令行執行,當命令行比較長的時候,可使用腳原本處理,比命令行的可讀性更高,還能夠加上註釋。
[root@iz2ze76ybn73dvwmdij06zz ~]# vim printname.awk
複製代碼
#!/usr/bin/awk -f
複製代碼
[root@iz2ze76ybn73dvwmdij06zz ~]# cat printname.awk
#!/usr/bin/awk -f #能夠加註釋了,哈哈 BEGIN { print "my name is Java小咖秀"} 複製代碼
[root@iz2ze76ybn73dvwmdij06zz ~]# chmod +x printname.awk
[root@iz2ze76ybn73dvwmdij06zz ~]# ll printname.awk -rwxr-xr-x 1 root root 60 7月 1 15:23 printname.awk 複製代碼
[root@iz2ze76ybn73dvwmdij06zz ~]# ./printname.awk
my name is Java小咖秀 複製代碼
瞭解了寫awk腳本的步驟之後你們就能夠本身去寫一波了~
相關內容還有不少,容我後續再分享~
後面還會繼續完善個人Linux實戰命令小手冊,持續更新中。。。
感謝小夥伴們的支持抱拳了
今天又到了一本書,持續更新。。⛽️
《awk介紹》
《Linux命令行與shell腳本》
《鳥哥的Linux私房菜》
《快樂的命令行》
百度百科
本文使用 mdnice 排版