03:linux文件操作四劍客


1.1 find查找命令

  1、find命令說明

      1. Linux find命令用來在指定目錄下查找文件。

      2. 任何位於參數之前的字符串都將被視為欲查找的目錄名。

      3. 如果使用該命令時,不設置任何參數,則find命令將在當前目錄下查找子目錄與文件。

-name            # 按文件名查找
-size            # 按文件大小查找
-perm            # 按權限查找
-mtime n         # 查找n天內修改內容的文件
-mmin n          # 查找n分鍾內修改內容的文件

  2、find常用查找方法

# 查找當前目錄下大於9M的文件詳細信息
[root@linux-node1 /]# find . -size +9M | xargs ls -lh
-rw-r--r--  1 root root  24M Jul  7 04:18 ./aaa/Python-3.7.0/libpython3.7m.a
-rwxr-xr-x  1 root root  14M Jul  7 04:19 ./aaa/Python-3.7.0/Programs/_testembed
-rwxr-xr-x  1 root root  14M Jul  7 04:18 ./aaa/Python-3.7.0/python
-rw-r--r--  1 root root  22M Jul  6 23:53 ./aaa/Python-3.7.0.tgz
-rw-------. 1 root root  47M Jan  7  2019 ./boot/initramfs-0-rescue-8b956f09fe0549c4b6182589acceab30.img
-rw-------. 1 root root  21M Jan  7  2019 ./boot/initramfs-3.10.0-514.el7.x86_64.img
-rw-------. 1 root root  14M Jan  7  2019 ./boot/initramfs-3.10.0-514.el7.x86_64kdump.img
find . -size +9M | xargs ls -lh : 查找當前目錄下大於9M的文件詳細信息
[root@linux-node1 /]# find . -type f -name "*.log" -size +1M -exec cp -av {} /tmp \;
cp: ‘./tmp/audit.log’ and ‘/tmp/audit.log’ are the same file
cp: ‘./tmp/journal.log’ and ‘/tmp/journal.log’ are the same file
find . -type f -name "*.log" -size +1M -exec cp -av {} /tmp \; 查找當前目錄下以 .log 結尾且大於5M的文件,並復制到/tmp目錄下
[root@linux-node1 /]# find /var -mtime +3 -mtime -5
/var/tmp
/var/lib/yum/yumdb/l
/var/lib/yum/yumdb/l/f20daac8f6b3893e42be72af22a5118848f
find /var -mtime +3 -mtime -5 在/var下查找更改時間在三天到五天的文件
[root@linux-node1 /]# find . -mmin +1 -mmin -3
./aa.py
find . -mmin +1 -mmin -3 查找當前文件夾下1分鍾前3分鍾內修改的文件

 1.2 grep指令

  1、grep指令說明     

      1. grep命令是一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹 配的行打印出來

      2. grep搜索成功,則返回0,如果搜索不成功,則返回1,如果搜索的文件不存在,則返回2。

^linux         # 以linux開頭的行
$php           # 以php結尾的行
.              # 匹配任意單字符
.+             # 匹配任意多個字符
.*             # 匹配0個或多個字符(可有可無)
[0-9a-z]       # 匹配中括號內任意一個字符
[abc]          # 表示匹配一個字符,這個字符必須是abc中的一個。
(linux)+       # 出現多次Linux單詞
(web){2}       # web出現兩次以上
\              # 屏蔽轉義  

   2、grep常用查找方法

[root@linux-node1 /]# grep -n 'root' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
grep -n 'root' /etc/passwd 查找/etc/passwd下包含 root字符串的文件
[root@linux-node1 /]# grep -Ev "root|nologin" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
grep -Ev "root|nologin" /etc/passwd 查找不包含root和nologin關鍵字的行
[root@linux-node1 /]# grep "root" /etc/{passwd,shadow}
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
grep "root" /etc/{passwd,shadow}查找/etc/passwd和/etc/shadow文件中包含root關鍵字的行
[root@linux-node1 /]# grep -c root /etc/passwd
2
grep -c root /etc/passwd 統計/etc/passwd文件中包含root字符串行的數量

  3、grep其他用法

[root@linux-node1 ~]# grep -E -v "^$|^#" /etc/nginx/nginx.conf 
user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
grep -E -v "^$|^#" /etc/nginx/nginx.conf : 去除空號和以#號開頭的行
[root@linux-node1 ~]# seq 1 20 |grep -m 5 -E '[0-9]{2}'
10
11
12
13
14
seq 1 20 |grep -m 5 -E '[0-9]{2}'輸出匹配的前五個結果
[root@linux-node1 ~]# seq 1 20 |grep -c -E '[0-9]{2}'
11
seq 1 20 |grep -c -E '[0-9]{2}'輸出匹配多少行
[root@linux-node1 ~]# echo "a bc de" |xargs -n1 |grep '^b'
bc
echo "a bc de" |xargs -n1 |grep '^b'匹配以字符串"b"開頭的行
[root@linux-node1 ~]# echo "a ab abc abcd abcde" |xargs -n1 |grep -n 'de$'
5:abcde
echo "a ab abc abcd abcde" |xargs -n1 |grep -n 'de$'匹配以"de"字符串結尾的行
[root@linux-node1 ~]# grep -r 'sshd' /etc --include *.conf           
/etc/sestatus.conf:/usr/sbin/sshd
/etc/sestatus.conf:/usr/sbin/sshd
grep -r 'sshd' /etc --include *.conf遞歸搜索/etc 目錄下包含 "sshd"字符串 的 conf 后綴文件
[root@linux-node1 ~]# seq 41 45 |grep -E '4[12]'
41
42
seq 41 45 |grep -E '4[12]'匹配41/42數字

 1.3 sed流編輯器,過濾和替換文本

  1、sed指令說明

      1. sed 命令將當前處理的行讀入模式空間進行處理,處理完把結果輸出,並清空模式空間。

      2. 然后再將下一行讀入模式空間進行處理輸出,以此類推,直到最后一行。

      3. 還有一個暫存空間,可以暫時存放一些處理的數據,但不能直接輸出,只能放到模式空間輸出。

      4. 這兩個空間其實就是在內存中初始化的一個內存區域,存放正在處理的數據和臨時存放的數據

-n         # 只列出sed處理的哪一行內容
-e         # 直接在sed模式上進行sed動作編輯
-f         # 直接將sed動作寫在一個文件內
-r         # 讓sed指令支持擴展的正則表達式
'''1. 常用選項 '''
-n   # 不打印模式空間
-e   # 執行腳本、表達式來處理
-f   # 執行動作從文件讀取執行
-i   # 修改原文件
-r   # 使用擴展正則表達式

'''2. 常用命令 '''
s/regexp/replacement/  # 替換字符串
p    # 打印當前模式空間
P    # 打印模式空間的第一行
d    # 刪除模式空間,開始下一個循環
D    # 刪除模式空間的第一行,開始下一個循環
=    # 打印當前行號
a \text    # 當前行追加文本
i \text    # 當前行上面插入文本
c \text    # 所選行替換新文本
q          # 立即退出 sed 腳本
r          # 追加文本來自文件
w filename # 寫入當前模式空間到文件
!          # 取反、 否定

'''3. 常用地址 '''
$              # 匹配最后一行
/regexp/       # 正則表達式匹配行
number         # 只匹配指定行
addr1,addr2    # 開始匹配 addr1 行開始,直接 addr2 行結束
addr1,+N       # 從 addr1 行開始,向后的 N 行
addr1,~N       # 從 addr1 行開始,到 N 行結束
sed指令參數詳解

  2、sed常用方法

[root@linux-node1 aaa]# nl /etc/passwd | sed '2,5d'
     1  root:x:0:0:root:/root:/bin/bash
     6  sync:x:5:0:sync:/sbin:/bin/sync
     7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8  halt:x:7:0:halt:/sbin:/sbin/halt
     9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10  operator:x:11:0:operator:/root:/sbin/nologin
nl /etc/passwd | sed '2,5d'將2~5行內容刪除,然后打印到屏幕上
[root@linux-node1 aaa]# nl /etc/passwd | sed '2,5c "new content"'
     1  root:x:0:0:root:/root:/bin/bash
"new content"
     6  sync:x:5:0:sync:/sbin:/bin/sync
     7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8  halt:x:7:0:halt:/sbin:/sbin/halt
     9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10  operator:x:11:0:operator:/root:/sbin/nologin
nl /etc/passwd | sed '2,5c "new content"'將2~5行的內容替換成字符串 "new content"
[root@linux-node1 aaa]# nl /etc/passwd | sed '/root/d'
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6  sync:x:5:0:sync:/sbin:/bin/sync
nl /etc/passwd | sed '/root/d'刪除/etc/passwd所包含root的行
[root@linux-node1 aaa]# nl /etc/passwd | sed -e '3,$d' -e 's/root/mewusername/'
     1  mewusername:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     
-e '3,$d'                       # 刪除/etc/passwd第三行到末尾的數據
-e 's/root/nuwusername/'        # 將搜索到的內容 root替換成 newusername
nl /etc/passwd | sed -e '3,$d' -e 's/root/mewusername/' 提取到前兩行數據,並將 root替換成 newusername

  3、sed匹配打印

[root@linux-node1 ~]# tail /etc/services |sed -n '/^blp5/p'
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
tail /etc/services |sed -n '/^blp5/p'打印匹配 blp5 開頭的行
[root@linux-node1 ~]# tail /etc/services |sed -n '1p'
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
tail /etc/services |sed -n '1p'打印第一行
[root@linux-node1 ~]# tail /etc/services |sed -n '1,3p'
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/tcp               # Image Systems Network Services
isnetserv       48128/udp               # Image Systems Network Services
tail /etc/services |sed -n '1,3p'打印第一行至第三行
[root@linux-node1 ~]# seq 10 |sed -n '1~2p'
1
3
5
7
9
seq 10 |sed -n '1~2p'打印奇數行
[root@linux-node1 ~]# tail /etc/services |sed -n '$p'
matahari        49000/tcp               # Matahari Broker
tail /etc/services |sed -n '$p'打印最后一行
[root@linux-node1 ~]# tail /etc/services |sed -n '$!p'
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/tcp               # Image Systems Network Services
isnetserv       48128/udp               # Image Systems Network Services
tail /etc/services |sed -n '$!p'不打印最后一行
[root@linux-node1 ~]# tail /etc/services |sed -n '/^blp5/,/^com/p'
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
com-bardac-dw   48556/tcp               # com-bardac-dw
tail /etc/services |sed -n '/^blp5/,/^com/p'匹配以"blp5開頭"到"com開頭"的所有行
[root@linux-node1 ~]# tail /etc/services |sed -n '/blp5/,$p'
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
com-bardac-dw   48556/tcp               # com-bardac-dw
tail /etc/services |sed -n '/blp5/,$p'匹配以"blp5"開頭行到最后一行
[root@linux-node1 ~]# a=1
[root@linux-node1 ~]# tail /etc/services |sed -n "$a,3p"
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/tcp               # Image Systems Network Services
isnetserv       48128/udp               # Image Systems Network Services
tail /etc/services |sed -n "$a,3p"引用系統變量,用引號

  4、sed匹配刪除

[root@linux-node1 ~]# tail /etc/services |sed '/blp5/d'
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/tcp               # Image Systems Network Services
isnetserv       48128/udp               # Image Systems Network Services
com-bardac-dw   48556/tcp               # com-bardac-dw
com-bardac-dw   48556/udp               # com-bardac-dw
tail /etc/services |sed '/blp5/d' : 刪除包含"blp5"的行
[root@linux-node1 ~]# tail /etc/services |sed '1d'
isnetserv       48128/tcp               # Image Systems Network Services
isnetserv       48128/udp               # Image Systems Network Services
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
com-bardac-dw   48556/tcp               # com-bardac-dw
tail /etc/services |sed '1d' : 刪除第一行
[root@linux-node1 ~]# tail /etc/services |sed '1~2d'
isnetserv       48128/tcp               # Image Systems Network Services
blp5            48129/tcp               # Bloomberg locator
com-bardac-dw   48556/tcp               # com-bardac-dw
iqobject        48619/tcp               # iqobject
matahari        49000/tcp               # Matahari Broker
tail /etc/services |sed '1~2d'刪除第一到第二行
[root@linux-node1 ~]# sed '/^#/d;/^$/d' /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
sed '/^#/d;/^$/d' /etc/nginx/nginx.conf去除空格或開頭#號的行

  5、sed匹配替換

[root@linux-node1 ~]#  tail /etc/services |sed 's/blp5/test/'
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/tcp               # Image Systems Network Services
isnetserv       48128/udp               # Image Systems Network Services
test            48129/tcp               # Bloomberg locator
test            48129/udp               # Bloomberg locator
com-bardac-dw   48556/tcp               # com-bardac-dw
com-bardac-dw   48556/udp               # com-bardac-dw
tail /etc/services |sed 's/blp5/test/'替換 blp5 字符串為 test
[root@linux-node1 ~]# tail /etc/services |sed -n 's/^blp5/test/p'
test            48129/tcp               # Bloomberg locator
test            48129/udp               # Bloomberg locator
tail /etc/services |sed -n 's/^blp5/test/p'替換開頭是 blp5 的字符串並打印
[root@linux-node1 ~]# tail /etc/services |sed 's/48049/&.0/'
3gpp-cbsp       48049.0/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/tcp               # Image Systems Network Services
isnetserv       48128/udp               # Image Systems Network Services
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
com-bardac-dw   48556/tcp               # com-bardac-dw
tail /etc/services |sed 's/48049/&.0/'使用&命令引用匹配內容並替換
[root@linux-node1 ~]# tail /etc/services | sed '1,4s/blp5/test/'
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/tcp               # Image Systems Network Services
isnetserv       48128/udp               # Image Systems Network Services
test            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
com-bardac-dw   48556/tcp               # com-bardac-dw
com-bardac-dw   48556/udp               # com-bardac-dw
iqobject        48619/tcp               # iqobject
iqobject        48619/udp               # iqobject
tail /etc/services | sed '1,4s/blp5/test/'將1到4行的"blp5"替換成"test"
[root@linux-node1 ~]# tail /etc/services | sed '/48129\/tcp/s/blp5/test/'
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/tcp               # Image Systems Network Services
isnetserv       48128/udp               # Image Systems Network Services
test            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
com-bardac-dw   48556/tcp               # com-bardac-dw
com-bardac-dw   48556/udp               # com-bardac-dw
iqobject        48619/tcp               # iqobject
iqobject        48619/udp               # iqobject
matahari        49000/tcp               # Matahari Broker
tail /etc/services | sed '/48129\/tcp/s/blp5/test/' : 匹配"48129/tcp"並將此行的"blp5"替換成"test"
[root@linux-node1 ~]# tail /etc/services |sed -e '1,2d' -e 's/blp5/test/'
isnetserv       48128/udp               # Image Systems Network Services
test            48129/tcp               # Bloomberg locator
test            48129/udp               # Bloomberg locator
com-bardac-dw   48556/tcp               # com-bardac-dw

[root@linux-node1 ~]# tail /etc/services |sed '1,2d;s/blp5/test/'  # 也可以使用分好分隔
isnetserv       48128/udp               # Image Systems Network Services
test            48129/tcp               # Bloomberg locator
test            48129/udp               # Bloomberg locator
com-bardac-dw   48556/tcp               # com-bardac-dw
com-bardac-dw   48556/udp               # com-bardac-dw
iqobject        48619/tcp               # iqobject
tail /etc/services |sed -e '1,2d' -e 's/blp5/test/' : 刪除前兩行並將"blp5"替換成"test"

  6、sed添加新內容

i: 匹配行上面添加
a: 匹配航下面添加
c: 將匹配航替換成新內容
[root@linux-node1 ~]# tail /etc/services |sed '/blp5/i \test'  #在 blp5 上一行添加 test
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/tcp               # Image Systems Network Services
isnetserv       48128/udp               # Image Systems Network Services
test
blp5            48129/tcp               # Bloomberg locator
test
blp5            48129/udp               # Bloomberg locator
com-bardac-dw   48556/tcp               # com-bardac-dw
tail /etc/services |sed '/blp5/i \test'在 blp5 上一行添加 test
[root@linux-node1 ~]# tail /etc/services |sed '/blp5/a \test'  # 在 blp5 下一行添加 test
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/tcp               # Image Systems Network Services
isnetserv       48128/udp               # Image Systems Network Services
blp5            48129/tcp               # Bloomberg locator
test
blp5            48129/udp               # Bloomberg locator
test
com-bardac-dw   48556/tcp               # com-bardac-dw
com-bardac-dw   48556/udp               # com-bardac-dw 
tail /etc/services |sed '/blp5/a \test'在 blp5 下一行添加 test
[root@linux-node1 ~]# tail /etc/services |sed '/blp5/c \test' 
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/tcp               # Image Systems Network Services
isnetserv       48128/udp               # Image Systems Network Services
test
test
com-bardac-dw   48556/tcp               # com-bardac-dw
com-bardac-dw   48556/udp               # com-bardac-dw
tail /etc/services |sed '/blp5/c \test'匹配"blp5"的行替換成"test"
[root@linux-node1 ~]# tail /etc/services |sed '2a \test'
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/tcp               # Image Systems Network Services
test
isnetserv       48128/udp               # Image Systems Network Services
blp5            48129/tcp               # Bloomberg locator
tail /etc/services |sed '2a \test' : 在第二行下面添加"test"

  7、文本操作

1 2 3
4 5 6
7 8 9
vim a.txt : 編寫一個測試文件
[root@linux-node1 ~]# tail /etc/services |sed '/blp5/r a.txt'
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/tcp               # Image Systems Network Services
isnetserv       48128/udp               # Image Systems Network Services
blp5            48129/tcp               # Bloomberg locator
1 2 3
4 5 6
7 8 9
blp5            48129/udp               # Bloomberg locator
1 2 3
4 5 6
7 8 9
com-bardac-dw   48556/tcp               # com-bardac-dw
com-bardac-dw   48556/udp               # com-bardac-dw
iqobject        48619/tcp               # iqobject
iqobject        48619/udp               # iqobject
matahari        49000/tcp               # Matahari Broker
tail /etc/services |sed '/blp5/r a.txt' : 將文件讀取追加到匹配行下面
[root@linux-node1 ~]# tail /etc/services |sed '/blp5/w b.txt'  #只有下面兩行被寫入
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
tail /etc/services |sed '/blp5/w b.txt'將匹配行寫入"b.txt"

1.4 awk指令

  1、awk指令說明

      1. awk是一種編程語言,用於在linux下對文本和數據進行處理

      2. awk的處理文件和數據處理方式是逐行掃描,尋找到匹配的行,並在這些行上進行你想要的操作

      3. 如果沒有指定處理動作,則把匹配的行顯示到屏幕上

//              # 匹配代碼塊,可以是字符串或正則表達式
{}              # 命令代碼塊,包含一條或多條命令
$0              # 表示整個當前行
$1              # 每行第一個字段
NF              # 字段數量變量
NR              # 每行的記錄號,多文件記錄遞增
/[0-9][0-9]+/   # 兩個或兩個以上數字
/[0-9][0-9]*/   # 一個或一個以上數字
-F'[:#/]'       # 定義三個分隔符
FNR             # 與NR類似,不過多文件記錄不遞增,每個文件都從1開始
\t              # 制表符
\n              # 換行符
FS              # BEGIN時定義分隔符
RS              # 輸入的記錄分隔符, 默認為換行符(即文本是按一行一行輸入)
~               # 匹配,與==相比不是精確比較
!~              # 不匹配,不精確比較
==              # 等於,必須全部相等,精確比較
!=              # 不等於,精確比較
&&             # 邏輯與
||              # 邏輯或
+               # 匹配時表示1個或1個以上

  2、awk常用指令

[root@linux-node1 aaa]# cat /etc/passwd |awk -F ':' '{print $1}'
root
bin
daemon
adm

-F ':'          # 使用 ":" 來分隔
'{print $1}'    # 打印第一列的數據
cat /etc/passwd |awk -F ':' '{print $1}' 以冒號為分隔打印第一列數據
[root@linux-node1 aaa]# cat /etc/passwd | awk -F ':' '{print $1"\t"$7}'
root    /bin/bash
bin     /sbin/nologin
daemon  /sbin/nologin
adm     /sbin/nologin
lp      /sbin/nologin
sync    /bin/sync

'{print $1"\t"$7}'        # 打印第一列和第七列數據,並以制表符分隔
cat /etc/passwd | awk -F ':' '{print $1"\t"$7}' 打印第一列和第七列數據,並以制表符分隔
[root@linux-node1 aaa]# awk -F ':'  '/root/{print $1}' /etc/passwd
root
operator
awk -F ':' '/root/{print $1}' /etc/passwd 搜索/etc/passwd有root關鍵字的所有行,只顯示第一行
[root@linux-node1 aaa]# w|awk 'NR==1{print $6}'
2
w|awk 'NR==1{print $6}'顯示第一行第六列的數據
[root@linux-node1 aaa]# awk -F: 'NR==5 || NR==6{print}' /etc/passwd 
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
awk -F: 'NR==5 || NR==6{print}' /etc/passwd 打印第五行和第六行數據
[root@linux-node1 aaa]# awk '!/mysql/' /etc/passwd    
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
awk '!/mysql/' /etc/passwd : 匹配所有 不包含 "mysql"關鍵字的行
[root@linux-node1 aaa]# awk '/mysql|mail/{print}' /etc/passwd 
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
awk '/mysql|mail/{print}' /etc/passwd : 匹配包含mysql 或者 mail的行

  3、awk基本語法

[root@linux-node1 ~]# tail -n3 /etc/services |awk -F'[ /]+' '{print $2}'  # 以空格或斜線分隔
48619
48619
49000
tail -n3 /etc/services |awk -F'[ /]+' '{print $2}' :以"空格"或"斜線"分隔
[root@linux-node1 ~]# awk -v a=123 'BEGIN{print a}'
123
awk -v a=123 'BEGIN{print a}' : 變量賦值
[root@linux-node1 ~]# tail -n3 /etc/services |awk 'BEGIN{print "服務\t\t端口\t\t\t描述"}{print $0}END{print "===結束==="}' 
服務            端口                    描述
iqobject        48619/tcp               # iqobject
iqobject        48619/udp               # iqobject
matahari        49000/tcp               # Matahari Broker
===結束===
tail -n3 /etc/services |awk 'BEGIN{print "服務\t\t端口\t\t\t描述"}{print $0}END{print "===結束==="}'BEGIN{} END{}結合使用

  4、正則匹配

[root@linux-node1 ~]# tail /etc/services |awk '/^blp5/{print $0}'
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
tail /etc/services |awk '/^blp5/{print $0}' 匹配開頭是 blp5 的行
[root@linux-node1 ~]# tail /etc/services |awk '/^[a-z0-9]{8} /{print $0}'
iqobject        48619/tcp               # iqobject
iqobject        48619/udp               # iqobject
matahari        49000/tcp               # Matahari Broker
tail /etc/services |awk '/^[a-z0-9]{8} /{print $0}' 匹配第一個字段是 8 個字符的行
[root@linux-node1 ~]# tail /etc/services |awk '/blp5/ || /tcp/{print $0}'
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/tcp               # Image Systems Network Services
blp5            48129/tcp               # Bloomberg locator
tail /etc/services |awk '/blp5/ && /tcp/{print $0}'匹配記錄中包含 blp5 或 tcp 的行
[root@linux-node1 ~]# awk '! /^#/ && ! /^$/{print $0}' /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
awk '! /^#|^$/' /etc/nginx/nginx.conf 不匹配開頭是#和空行
[root@linux-node1 ~]# tail /etc/services |awk '/^blp5/,/^com/'
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
com-bardac-dw   48556/tcp               # com-bardac-dw
tail /etc/services |awk '/^blp5/,/^com/'匹配以 "blp5開頭" 到 "com開頭" 之間的所有行

  5、NF

[root@linux-node1 ~]# echo "a b c d e f" |awk '{print NF}'
6
echo "a b c d e f" |awk '{print NF}'打印行數
[root@linux-node1 ~]# echo "a b c d e f" |awk '{print $1}'
a
echo "a b c d e f" |awk '{print $1}'打印第一行
[root@linux-node1 ~]# echo "a b c d e f" |awk '{print $NF}' : 打印最后一行
f
echo "a b c d e f" |awk '{print $NF}' : 打印最后一行
[root@linux-node1 ~]# echo "a b c d e f" |awk '{print $(NF-1)}' :打印倒數第二行
e
echo "a b c d e f" |awk '{print $(NF-1)}'打印倒數第二行
[root@linux-node1 ~]# echo "a b c d e f" |awk '{$NF="";$(NF-1)="";print $0}' :排除最后兩個字段
a b c d  
echo "a b c d e f" |awk '{$NF="";$(NF-1)="";print $0}' 排除最后兩個字段

  6、NR

[root@linux-node1 ~]# tail -n5 /etc/services |awk '{print NR,$0}' :打印行號+內容
1 com-bardac-dw   48556/tcp               # com-bardac-dw
2 com-bardac-dw   48556/udp               # com-bardac-dw
3 iqobject        48619/tcp               # iqobject
tail -n5 /etc/services |awk '{print NR,$0}' 打印行號+內容
[root@linux-node1 ~]# tail -n5 /etc/services |awk 'NR==3{print $2}' :打印第三行第二列的值
48619/tcp
tail -n5 /etc/services |awk 'NR==3{print $2}' 打印第三行第二列的值
[root@linux-node1 ~]# tail -n5 /etc/services |awk 'NR<=3{print NR,$0}' :打印前三行
1 com-bardac-dw   48556/tcp               # com-bardac-dw
2 com-bardac-dw   48556/udp               # com-bardac-dw
3 iqobject        48619/tcp               # iqobject
tail -n5 /etc/services |awk 'NR<=3{print NR,$0}'打印前三行

  7、操作符

'''在 awk 中,有 3 種情況表達式為假:數字 0,空字符串和未定義的值'''
[root@linux-node1 ~]# awk 'BEGIN{n=0;if(n)print "true";else print "false"}'
[root@linux-node1 ~]# awk 'BEGIN{s="";if(s)print "true";else print "false"}'
[root@linux-node1 ~]# awk 'BEGIN{if(s)print "true";else print "false"}'
awk三種為假的情況數字 0,空字符串和未定義的值
[root@linux-node1 ~]# seq 3 |awk '{print $0*2}' : 乘法
2
4
6
seq 3 |awk '{print $0*2}' : 乘法
[root@linux-node1 ~]# seq 3 |awk '{print $0/2}' :除法
0.5
1
1.5
seq 3 |awk '{print $0/2}'除法
[root@linux-node1 ~]# seq 5 |awk '$0%2==0{print $0}' :取余
2
4
seq 5 |awk '$0%2==0{print $0}'取余
[root@linux-node1 ~]# seq 5 |shuf |awk '{print $0|"sort"}' :先打亂再排序
1
2
3
4
5
seq 5 |shuf |awk '{print $0|"sort"}'先打亂再排序

  8、流程控制 (if 語句 )

      if (condition) statement [ else statement ]

[root@linux-node1 ~]# seq 5 |awk '{if($1==3)print $0}' :如果第一列的值等於3,打印出來
3
seq 5 |awk '{if($1==3)print $0}' 如果第一列的值等於3,打印出來
[root@linux-node1 ~]# echo "123abc#456cde 789aaa#aaabbb aaaa#eee666eeee " |xargs -n1 |awk -F# '{if($2~/[0-9]/)print $2}'  # $2~/[0-9]/ (如果第二行匹配到數字就打印)
456cde
eee666eeee
if+正則匹配
[root@linux-node1 ~]# seq 5 |awk '{if($0==3)print $0;else print "no"}' :雙分支if
no
no
3
no
no
seq 5 |awk '{if($0==3)print $0;else print "no"}'雙分支if
[root@linux-node1 ~]# awk 'BEGIN{a["a"]=123}END{if("a" in a)print "yes"}' < /dev/null :判斷數組成員 
yes
awk 'BEGIN{a["a"]=123}END{if("a" in a)print "yes"}' < /dev/null判斷數組成員
[root@linux-node1 ~]# awk 'BEGIN{print 1==1?"yes":"no"}' :三目運算符 
yes
awk 'BEGIN{print 1==1?"yes":"no"}'三目運算符

  9、多分支if、for循環、while語句

1 2 3
4 5 6
7 8 9
vim file : 編輯一個測試文件
[root@linux-node1 ~]# awk '{i=1;while(i<=NF){print $i;i++}}' file
1
2
3
4
5
6
7
8
9
awk '{i=1;while(i<=NF){print $i;i++}}' file遍歷文件內容
[root@linux-node1 ~]# awk '{for(i=1;i<=NF;i++)print $i}' file
1
2
3
4
5
6
7
8
9
awk '{for(i=1;i<=NF;i++)print $i}' filefor循環遍歷
[root@linux-node1 ~]# awk '{for(i=NF;i>=1;i--)print $i}' file
3
2
1
6
5
4
9
8
7
awk '{for(i=NF;i>=1;i--)print $i}' file倒序打印
[root@linux-node1 ~]# awk 'BEGIN{for(i=1;i<=5;i++){if(i==3){break};print i}}'
1
2
awk 'BEGIN{for(i=1;i<=5;i++){if(i==3){break};print i}}' break語句
[root@linux-node1 ~]# awk 'BEGIN{for(i=1;i<=5;i++){if(i==3){continue};print i}}'
1
2
4
5
awk 'BEGIN{for(i=1;i<=5;i++){if(i==3){continue};print i}}'continue語句

  10、數組

[root@linux-node1 ~]# awk 'BEGIN{a[0]="test";print a[0]}'
test
awk 'BEGIN{a[0]="test";print a[0]}'自定義數組
[root@linux-node1 ~]# tail -n5 /etc/passwd |awk -F: '{a[NR]=$1}END{for(v in a)print a[v],v}'
saltapi 4
nginx 5
sshd 1
chrony 2
tcpdump 3
tail -n5 /etc/passwd |awk -F: '{a[NR]=$1}END{for(v in a)print a[v],v}'for循環遍歷數組
[root@linux-node1 ~]# tail /etc/services |awk '{a[$1]++}END{for(v in a)print a[v],v}'
2 com-bardac-dw
1 3gpp-cbsp
2 iqobject
1 matahari
2 isnetserv
2 blp5
tail /etc/services |awk '{a[$1]++}END{for(v in a)print a[v],v}'統計相同字段出現次數
[root@linux-node1 ~]# netstat -antp |awk '/^tcp/{a[$6]++}END{for(v in a)print a[v],v}'
11 LISTEN
3 ESTABLISHED
netstat -antp |awk '/^tcp/{a[$6]++}END{for(v in a)print a[v],v}' :統計 TCP 連接狀態(並發數)
[root@linux-node1 ~]# tail /etc/services |awk '{a[$1]++}END{for(v in a) if(a[v]>=2){print a[v],v}}'
2 com-bardac-dw
2 iqobject
2 isnetserv
2 blp5
tail /etc/services |awk '{a[$1]++}END{for(v in a) if(a[v]>=2){print a[v],v}}'只打印出現次數大於等於 2 的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM