awk(報告生成器),grep(文本過濾器),sed(流編輯器)使用入門

三劍客

linux下的文本三劍客linux

grep

egrep,grep,fgrep 
文本查找的須要
grep:根據模式搜索文本,並將符合模式的文本行顯示出來。
pattern:文本符和正則表達式的元字符組合而成的匹配條件

grep [option] "pattern" file 
grep root /etc/passwd

-i:忽略大小寫 
--color:匹配的字符高亮顯示  alias
alias  grep='grep --color'
-v:反向查找 
-o:只顯示被模式匹配的字符串(不顯示行)
複製代碼

globbing

*:任意長度的任意字符
?:任意單個字符
[]:任意一個字符
[^]:其中任意一個非 
複製代碼

正則表達式:Regular ExPression,REGEXP

元字符:
.:匹配任意單個字符
[]:匹配指定範圍內的任意字符
[^]:匹配指定範圍內的任意單個字符
[:digit:][:lower:][:upper:] []

字符匹配次數:
*:表示匹配前面的字符任意次(0-inf)
   a*b 
   a.*b
.*:表示任意長度的,任意字符
工做在貪婪模式 
\?:匹配其前面的字符一個或0次。
    部分匹配 
  a?b 
\{m,n\}:匹配其前的字符至少m,至多n次。
   \{1,\}
  \{0,3\}
  a\{1,3\}
  a.\{1,3\}
複製代碼

位置錨定:

^:錨定行首,此字符後面的任意內容必須出如今行首。
grep "^root" /etc/passwd 

$:錨定行尾,此字符前面的任意內容必須出如今行尾。

grep "bash$" /etc/passwd 
^$:空白行 
grep '^$' /etc/passwd 
複製代碼

數字:

[0-9]:

grep "[[:space:]][[:digit:]]$" 

r555t 
複製代碼

錨定單詞:

\<或\b:其後面的任意字符必須出如今行首
\>或\b:其前面的任意字符必須出如今行尾。

This is root.
The user is mroot
rooter is dogs name.
chroot is a command.
grep "root\>" test.txt 
grep "\<root" test.txt 
grep "\<root\>" test.txt  
複製代碼

分組:

\(\)
\(ab\)* :ab一個總體 
  
  後向引用
  
He love his lover.
She like her liker.
He  love his liker.
She like her lover.

grep 'l..e*l..e*' text.txt 
grep "l..e.*\1" text.txt
grep "\(l..e\)" 

\1:調用第一個左括號以及與之對應的右括號之間的內容。
\2:
\3:

/etc/inittab 
grep '\([0-90]\).*\1$'  /etc/inittab 
複製代碼

REGEXP:regular Expresssion

pattern:文本的過濾條件git

正則表達式: basic REGEXP:基本正則表達式 Extent REGEXP :擴展正則表達式正則表達式

基本正則表達式shell

.
[]
[^]

次數匹配:
*:
\?:0或1次
\{m,n\}:至少m次,至多n次

.*:

錨定:
^:
$:
\<,\b: 
\>,\b:

\(\)
\1,\2....
複製代碼

grep:使用基本的正則表達式定義的模式來過濾文本的命令:

-i:忽略大小寫 
-v 
-o 
--color 

-E 支持擴展的正則表達式 
-A  # :顯示匹配行及之後多少行也顯示 
  after 
-B:顯示匹配行以及前面的n行
   before 
-C:顯示匹配行以及先後的n行
   contest 
grep -A 2 ""  file 


擴展正則表達式:
   貪婪模式

字符匹配:
.
[]
[^]

次數匹配:
*:
?:
+:匹配其前面的字符至少一次
{m,n}

位置錨定:
^
$
\<
\>

分組:
():分組
\1,\2,\3.....

或者:
a|b  or 

C|cat: 
(C|c)at: 

grep --color -E '^[[:space:]]+' /boot/grub/grub.conf 

grep -E = egrep 

egrep --color '\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-9]|25[0-5])\>' 

(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-9]|25[0-5])\>\.){3}'\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-9]|25[0-5])\>\.'

IPV4:
5類:
A B C D E 
A:1-127 
B:128-191 
C: 192--223 

\<[1-9]|[1-9][0-9]|1[0-9]{2}|2[01][0-9]|22[0-30]\>
複製代碼

sed(流編輯器)

sed基本用法:

sed:stream Editor 
行編輯器 
   文本編輯器 
   逐行處理文本 
  
全屏編輯器:vim 
 
內存空間:模式空間 
sed 模式空間 
匹配模式空間後,進行操做,將結果輸出。僅對模式空間中的數據進行處理,然後,處理結束,將模式空間打印至屏幕;

默認sed不編輯原文件,僅對模式空間中的數據進行處理。
複製代碼

sed [option] [sed-scripts]

option:
-n:靜默模式 
-i:直接修改原文件
-e scripts -e script:能夠同時執行多個腳本。
-f /path/to/sed_scripts  命令和腳本保存在文件裏調用。
  sed -f /path/to/scripts  file 
-r:表示使用擴展的正則表達式。
   只是進行操做,不顯示默認模式空間的數據。
複製代碼

comamnd:

address:指定處理的行範圍

sed 'addressCommand' file ... 
對符合地址範圍進行操做。
Address: 
1.startline,endline 
 好比1,100
   $:最後一行
2./RegExp/ 
  /^root/
3./pattern1/,/pattern2/ 
  第一次被pattern匹配到的行開始,至第一次pattern2匹配到的行結束,這中間的全部行。
4.LineNumber 
   指定行 
5.startline,+N 
 從startline開始,向後的N行。
 
Command:
 d:刪除符合條件的行。
     sed '3,$d' /etc/fstab
     sed '/oot/d' /etc/fstab 
注意:模式匹配,要使用 // 
    sed '1d' file 
p:顯示符合條件的行 
 sed '/^\//d' /etc/fstab 
 sed '/^\//p' /etc/fstab 
   會顯示兩次
    先顯示P匹配,再顯示全部模式空間的數據。
a \string:在指定的行後面追加新行,內容爲"string"
sed '/^\//a \# hello world' /etc/fstab 
添加兩行:
sed '/^\//a \#hello world \n #hi' /etc/fstab 

i \sting:在指定行的前面添加新行,內容爲string。

r file:將指定的文件的內容添加在指定行後面。
  sed '2r /etc/issue'   /etc/fstab 
  sed '$r /etc/issue' /etc/fstab 

w file:將地址指定的範圍的內容另存至另外一文件中。
 sed '/oot/w /tmp/oot.txt' /etc/fstab 
 
s/pattern/string/:查找並替換 
     sed  's/oot/OOT/'  /etc/fstab 
sed 's/^\//#/' /etc/fstab 
sed 's/\//#/'/etc/fstab 僅替換每一行第一次被模式匹配的串。
  加修飾符 
   g:全局替換 
   i:忽略大小寫 
 sed 's/\//#/g'/etc/fstab
 
 s///:s###
 s@@@
 
sed 's#+##' 

後向引用

l..e:like----->liker 
     love----->lover 
	 
sed 's#l..e#&r#' file
&:表示模式匹配的引用 

sed 's#l..e#\1r#' file 

like---->Like
love---->Love 
sed 's#l\(..e\)#L\1#g' file 


history |sed 's#[[:space:]]##g'
history | sed 's#^[[:space:]]##g'

sed ''dirname
複製代碼

例子🌰

1.刪除/etc/grub.conf文件中行首的空白符;
 sed  's/^[[:space:]]+//g' /etc/grub.conf 
 2.替換/etc/inittab文件中"id:3:initdefault:"一行中的3
 sed 's#id:3:init#id:5:initd#'
 sed 's@\(id:\)[0-9]\(:initdefault:\)@\15\2@g' /etc/inittab 
 3.刪除/etc/inittab文件中的空白行。
  sed '/^$/d' /etc/inittab
4.刪除/etc/inittab文件中開頭的#號
sed 's/^#//'  
5.刪除莫文件中開頭的#號以及空白行。
sed 's/^[[:space:]]+//g' 
6.刪除某文件中以空白字符後面跟#類的行中開頭的空白字符以及#
sed -r 's/^[[:space:]]+#//g' 
7.取出一個文件路徑的目錄名稱
echo '/etc/rc.d'|sed -r 's@^(/.*/)[^/]+/?@\1@g'
複製代碼

awk(報告生成器)

grep :文本過濾器
sed:流編輯器 


grep option pattern file 
sed addresscommmand file 
sed 'comand/pattern/' file 
複製代碼

awk(報告生成器)vim

根據定義好的格式,顯示出來。
nawk 
gawk
gnu awk 

awk option 'script' file file2 
awk [option] 'pattern {action}' file file2 

print 
printf 自定義顯示格式


awk一次抽取一行,而後對每一行進行切割分片,每一片能夠使用變量進行引用。
$0:表示引用一整行
$1:第一個切片
$2:第二個切片 

awk '{print $1}' text.txt 
awk '{print $1,$2}' text.txt
複製代碼

選項:

-F  指定分隔符
awk -F ''

awk 'BEGIN{OPS="#"}{print $1,$2}' test.txt
BEGIN{OPS=""} 輸出分隔符

輸出特定字符
awk '{print $1,"hello",$2,$3,$4,$5}' file 

awk 'BEGIN{print "line one\nline two\nline tree"}'

print的格式:
print item1,item2...

awk -F: 輸入分隔符 
OFS="#"   輸出分隔符
複製代碼

awk變量

awk內置變量
FS: filed separator,讀取文本時,所用字段分隔符
RS:recordsepartor,輸入文本信息所使用的換行符。
OFS:OUT filed separator 
ORS:Output ROw separator 

awk -F:
OFS="#"
FS=":"
複製代碼

awk內置變量之數據變量

NR: the number of input record ,awk命令所處理的記錄,若是有多個文件,這個數據是全部處理的行數。
FNR:當前文件所處理的行是本文件第多少行。
NF:當前所處理的行有多少個字段。


awk '{print NF}' file 
awk '{print $NF}' file 
awk '{print NR}' file 
複製代碼

-v 定義變量

awk -v test="hello awk" '{print test}' 
awk -v test="hell awk" 'BEGIN{print test}'


awk  'BEGIN{test='hello awk',print test}'
複製代碼

printf 格式化顯示

printf  format,item1,item2...

awk 'BEGIN{printf %c,}'
注意:printf不換行  

%d 
%e 
%f 
%g 

修飾符
-:左對齊 
%nd:顯示寬度 
awk '{printf %-10s%-10s\n,$1,$2}' file
複製代碼

awk的操做符 算術操做符 字符串操做符 布爾表達式數組

x < y 
x <= y 
x > y 
x != y 
x ~ y 匹配 
x !~ y 
複製代碼

表達式間的邏輯關係符

&& 
|| 
複製代碼

條件表達式

select?if-true-exp:if-false-exp 
a>b?a=1:b=2 
複製代碼

awk模式

1.正則表達式 /pattern/
2.表達式 
3.REGEXP 指定匹配範圍 
4.BEGIN/END 
5Empty  


awk -F : '/^r/ {print $1}' /etc/passwd 
awk -F : '$3>=500{printf $1,$3}' /etc/passwd 
awk -F: '$3+1>=500{print $1,$3}' /etc/passwd

awk -F: '$7~"bash$"{print $1,$7}' /etc/passwd 
進行匹配測試
awk -F: '$7!~"bash$"{print $1,$7}' /etc/passwd 

awk -F: '/^r/,/^m/{print $1,$7}' /etc/passwd 

awk -F: '$3==0,$7~"bash"{print $1,$3,$7}' /etc/passwd 

awk -F '{printf "%-10s%-10s%-20s\n",$1,$2,$3}' /etc/passwd 

BEGIN ,END 

awk -F: '$3==0,$7~"nologin"BEGIN{print "Username ID shell"}{printf "%-10s%-10s%-20s\n"$1,$3,$7} END{print "ending"}' /etc/passwd 
複製代碼

action

1.ExPression 
2.control statements 
3.compound statements 
4.INput statment 
5 output statements 
複製代碼

控制語句

if-else

if(condition) {then-body} else {[else-body]}
eg:
awk -F:
複製代碼
while
while (condition){statement1;statement2;...}
循環每一字段 
length([string])

awk -F: '{i=1; while (1<=NF) if {(length($i)>4) {print $i}; i++}}'

df -hP |awk '{if($4 >=) Print $0}'


do while 
do{statement1,statement2,...} while(condition)

for 
for( ; ; ){statement1;statement2....}

awk -F: '{for(i=1:i<=NF;i++){if(length($i)>=4){print $i}}}'  /etc/passwd 

case 
switch (exprssion) {case value or /regexp/:statement1,statement2,...default:statement,....}
 
breakcontinue 
contine是遍歷字段的 

next 
提早結束對本行文本的處理,並接着處理下一行,
複製代碼

數組

數組下表是從1開始的
awk[mon]=1 
awk[tus]=2 


for (var in arrary){statement,....}

awk -F: '{shell[$NF]++}END {for(A in shell) {print A,shell[A]}}' /etc/passwd 

nestat -tan 

netstat -tan |awk '/^tcp/{STATE[$NF]++}END{for (S in STATE){print S,STATE[S]}}'

awk '{count[$1]++}END{for ip in count}{printf "%-20s:%d\n",ip,count[ip]}}'  access_log 
複製代碼
相關文章
相關標籤/搜索