linux shell腳本攻略 第四章 讓文本飛 grep,cut,sed,awk,paste

1.正則表達式

正則表達式手冊:https://tool.oschina.net/uploads/apidocs/jquery/regexp.html

  • 舉例

    匹配郵箱:[a-z0-9_]+@[a-z0-9]+.[a-z]+
    匹配所有單詞:( ?a-zA-z ?)
    匹配ip:[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}

  • 處理特殊字符
    特殊字符包括$,+,^,*,.等
    加反斜槓\,如a.txt在正則中爲a.txt

2.使用grep搜索文本

  • 常用選項
    grep word:搜索包含模式的匹配行
    -o:只輸出匹配到的部分
    -v:反轉匹配結果,打印沒有匹配到的行
    -c:統計匹配行數
    -n:打印出行號和行
    -b:打印出字節偏移,總是和-o一起使用
    -l:搜索多個文件並輸出匹配模式在哪個文件
    -R:遞歸搜索
    -i:忽略大小寫
    -e:指定多個模式,grep -e pattern1 -e pattern2
    -f:從文件中讀取模式
    -Z:輸出以’\0’字節作爲終止字符
    -q:靜默輸出,用於檢測搜索結果
    -ABC 5:打印前,後,前後5行

    grep -o|wc -l:輸出匹配次數
    –include:包含那些文件,例如–include *.{cpp,c}
    –exclude:不包含哪些文件,例如–exclude *.{md, git}
    –color=auto:着重標記匹配到的單詞
    grep -E或者egrep:使用正則表達式匹配

  • 舉例

Ian>grep maybe a.txt 
It's maybe the slowest bus Which colour for her is right
It's maybe the slowest bus
Ian>grep  -E "[W-Z]+" a.txt 
It's maybe the slowest bus Which colour for her is right
You tell me truth will not be here
Which colour for her is right
You tell me they are tough and red
Ian>grep  -v i a.txt 
And I see all the teenagers'eyes you tell me they are tough and red
It's maybe the slowest bus
And I see all the teenagers'eyes
You tell me they are tough and red
Ian>grep  -o up a.txt 
up
Ian>grep  -c it a.txt 
10
Ian>grep  -o u a.txt |wc -l
26
Ian>grep  -n up a.txt
8:Put up you in sandwiches hands oh I think it not really cool
Ian>grep  -b -o up a.txt
366:up
Ian>grep -l "it" a.txt b.txt 
a.txt
b.txt
Ian>grep -R -n "up" 
out.html:51:		 Charsets / OS/2 support © 2001 by Kyosuke Tokoro
grep/a.txt:8:Put up you in sandwiches hands oh I think it not really cool
grep/b.txt:8:Put up you in sandwiches hands oh I think it not really cool
Ian>grep -i "RED" a.txt 
And I see all the teenagers'eyes you tell me they are tough and red
You tell me they are tough and red
Ian>grep -e "up" -e "red" a.txt 
And I see all the teenagers'eyes you tell me they are tough and red
Put up you in sandwiches hands oh I think it not really cool
You tell me they are tough and red
Ian>cat pat_file 
you
up
Ian>grep -f pat_file a.txt 
And I see all the teenagers'eyes you tell me they are tough and red
Say say it again you know the past things could set my free
Put up you in sandwiches hands oh I think it not really cool
I've found it in your eyes
Say say it again you know the past things could set my free
Say say it again you know the past things could set my free
Say say it again you know the past things could set my free
Ian>grep -q "dada" a.txt 
Ian>echo $?
1

3.使用cut按列切分文件

  • 常用命令
    -d:指定定界符
    -f :指定字段
    -c:指定字符
    -b:指定字節
    -c/f/b 2,3:指定第2.3個字符,字段,字節
    -c/f/b 2-4:指定第2-4個字符,字段,字節
    -c/f/b -4:指定1-4個字符,字段,字節
    -c/f/b 5-:指定第5到最後一個字符,字段,字節
    –output-delimiter “,”:指定多個字段時的分隔符,比如-c 2-4,5-6 --output-delimiter “,”
    –complement:提取補集
  • 舉例
Ian>cat a.txt 
1 2 3 4 5 6 
2 4 6 8 9 11
3 6 9 11 12 13
Ian>cut -d " " -f 2-3 a.txt 
2 3
4 6
6 9
cut -d " " -f 2-3,4-5 a.txt --output-delimiter "."
2.3.4.5
4.6.8.9
6.9.11.12
cut -c 2-3,4-8 a.txt --output-delimiter "."
 2. 3 4 
 4. 6 8 
 6. 9 11
Ian>cut -c 2-3,4- a.txt --output-delimiter "."
 2. 3 4 5 6 
 4. 6 8 9 11
 6. 9 11 12 13
Ian>cut -b 2-6,7- a.txt --output-delimiter "...."
 2 3 ....4 5 6 
 4 6 ....8 9 11
 6 9 ....11 12 13

4.使用sed進行替換

sed命令詳解:https://www.cnblogs.com/ftl1012/p/9250171.html
此處只講替換功能

  • 常用方法
    • 常見參數
      參數s:替換文本,替換命令用替換模式替換指定模式
      參數g:替換行中所有的匹配,如果想從第2個處替換:2g
      定界符:可以任意指定定界符,如替換成":“或者”|",定界符出現在樣式內部時需要使用""轉義
    • 常用命令
      -e:允許多項編輯,如sed -e “s/a/b/g” -e “s/c/d/g” file
      -i:將替換直接寫入原文件
    • 舉例
Ian>cat a.txt 
aa ab ac ad aa aa
Ian>sed "s/aa/SS/" a.txt #只修改第一個
SS ab ac ad aa aa
Ian>sed "s/aa/SS/2g" a.txt #從第二個開始修改
aa ab ac ad SS SS
Ian>sed -e "s/aa/SS/" -e "s/ab/DD/" a.txt #選擇多個表達式
SS DD ac ad aa aa
Ian>sed -i "s/aa/SS/g" a.txt 
Ian>cat a.txt 
SS ab ac ad SS SS
Ian>sed  "sa\adaSAag" a.txt #定界符爲a,出現在內容中,需要轉義
SS ab ac SA SS SS
Ian>cat b.txt 
1221

12312
Ian>sed "/^$/d" b.txt #刪除空白行
1221
12312
  • 高級用法

    • sed -ibak:直接替換內容,且生成一個原文件的副本file.bak
    • &:標記匹配到的字符串
    • \1,\2:標記匹配到的第一個,第二個
    • 雙引號:可以使用變量
    • 舉例
Ian>sed -i.bakfile "s/ad/dwdwd/g" a.txt #添加bak文件
Ian>ls
a.txt  a.txt.bakfile  b.txt
Ian>cat a.txt
SS ab ac dwdwd SS SS
Ian>sed "s/ac/[&]/g" a.txt #&標記匹配到的字符串
SS ab [ac] dwdwd SS SS
Ian>cat a.txt
SS ab ac dwdwd SS SS
aa SS aa SS
Ian>sed 's/\([a-z]\+\)\+ \([A-Z]\+\)/\2 \1/' a.txt #獲取匹配到的參數
SS ab ac SS dwdwd SS
SS aa aa SS
Ian>text="hello"
Ian>echo "hello world"|sed "s/$text/hhh/g"
hhh world

5.awk高級文本處理

  • 基本使用:

    awk "BEGIN { begin statements } pattern { commands } END { end statements }"

Ian>awk "BEGIN { i=0 } { i++ } END { print i }" b.txt 
3
print參數以逗號分隔,打印時按照空格分隔;print中的雙引號爲連接符用
an>echo | awk "{ v1=\"v1\"; v2=\"v2\"; print v1,v2;}"
v1 v2
Ian>echo | awk "{ v1=\"v1\"; v2=\"v2\"; print v1 \"-\" v2;}"
v1-v2
  • 工作流程:

    1.執行BEGIN中的命令
    2.執行pattern {command}中的命令,按行執行
    3.執行END中的命令

  • 特殊變量

    NR:行號
    NF:當前行的字段數
    $0:當前行的內容
    $1:當前行的第一個字段
    $2:當前行的第二個字段

    • 特殊變量過濾
      NR<5:行號小於5
      NR=.=1;NR==4:1到4行
      ‘/linux/’:包含模式linux的行
      ‘!/linux/’:不包含模式linux的行
    • 舉例
Ian>echo -e "line1 f2 f3\nlin2 f4 f5" | awk '{print "Line no:"NR"  Line field:"NF"  $0="$0"  $1="$1"  $2="$2 "  $(NF-1)=" $(NF-1)}'
Line no:1  Line field:3  $0=line1 f2 f3  $1=line1  $2=f2  $(NF-1)=f2
Line no:2  Line field:3  $0=lin2 f4 f5  $1=lin2  $2=f4  $(NF-1)=f4
Ian>echo -e "line1 f2 f3\nlin2 f4 f5" | awk ' NR==1 { print $2 }'#只打印第一行
f2
Ian>awk "END { print NR }" out.html #統計行數
54
Ian>seq 3 | awk ' BEGIN { sum=0; print sum } { print ; sum=sum+$1 } END { print sum }'#加和
0
1
2
3
6
Ian>seq 3 | awk ' BEGIN { sum=0; print sum } NR==1,NR==2 { print ; sum=sum+$1 } END { print sum }'只對1-2行加和
0
1
2
3
Ian>seq 3 | awk ' BEGIN { sum=0; print sum } /2/ { print ; sum=sum+$1 } END { print sum }'#只對含有2的行執行
0
2
2
Ian>seq 3 | awk ' BEGIN { sum=0; print sum } !/2/ { print ; sum=sum+$1 } END { print sum }'#只對不含有2的行執行
0
1
3
4
Ian>echo -e "line1 f2 f3\nlin2 f4 f5" | awk '  { if(NR%2==1) print $2; }'#取餘
f2

  • 高級用法
    • 將外部變量傳給awk
    • getline讀取行
    • 使用循環
    • 內建字符串函數:length(str),index(str,search_str),split,substr,sub,gsub,match等函數
Ian>v1=1111
Ian>v2=2222
Ian>echo | awk " { print v1,v2 }" v1=$v1,v2=$v2#傳入外部變量
1111,v2=2222
Ian>seq 3|awk 'BEGIN { getline; print "getline:"$0 } { print ; }'#獲取一行
getline:1
2
3
Ian>echo |awk '{ "grep root /etc/passwd" |getline output; print output }'#使用getline將數據輸出傳給output變量
root:x:0:0:root:/root:/bin/bash
Ian>echo |awk '{ for(i=0;i<3;i++) print i; }'#使用for循環
0
1
2

6.按列合併文件(paste)

paste fille1 file2 -d “,”:按列合併兩個文件,用","當做分隔符

Ian>cat f1 
1
2
3
Ian>cat f2
4
5
6
7
Ian>paste f1 f2 -d "."
1.4
2.5
3.6
.7

7.打印兩個pattern之間的文本

awk ’ /start_pattern/, /end_pattern/’ file

Ian>cat f1 
1
2
3
Ian>awk '/1/,/2/' f1
1
2

8.倒序打印文本

  • tac命令
    tac是反過來的cat
Ian>cat f1
1
2
3
Ian>tac f1
3
2
1
  • 使用awk實現棧
Ian>awk '{ stack[NR]=$0 } END { for(i=NR;i>0;i--) print stack[i]; }' f1
3
2
1

9.替換目錄中所有文件的文本

Ian>cat test.txt 
a s d f
Ian>find ./ -name *.txt -print0| xargs -I{} -0 sed -i "s/a/b/g" {}
Ian>cat test.txt 
b s d f
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章