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

[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

[root@linux-node1 /]# find /var -mtime +3 -mtime -5 /var/tmp /var/lib/yum/yumdb/l /var/lib/yum/yumdb/l/f20daac8f6b3893e42be72af22a5118848f

[root@linux-node1 /]# find . -mmin +1 -mmin -3 ./aa.py
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

[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

[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

[root@linux-node1 /]# grep -c root /etc/passwd 2
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; }

[root@linux-node1 ~]# seq 1 20 |grep -m 5 -E '[0-9]{2}' 10 11 12 13 14

[root@linux-node1 ~]# seq 1 20 |grep -c -E '[0-9]{2}' 11

[root@linux-node1 ~]# echo "a bc de" |xargs -n1 |grep '^b' bc

[root@linux-node1 ~]# echo "a ab abc abcd abcde" |xargs -n1 |grep -n 'de$' 5:abcde

[root@linux-node1 ~]# grep -r 'sshd' /etc --include *.conf /etc/sestatus.conf:/usr/sbin/sshd /etc/sestatus.conf:/usr/sbin/sshd

[root@linux-node1 ~]# 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 行結束
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

[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

[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

[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
3、sed匹配打印

[root@linux-node1 ~]# tail /etc/services |sed -n '/^blp5/p' blp5 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator

[root@linux-node1 ~]# tail /etc/services |sed -n '1p' 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol

[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

[root@linux-node1 ~]# seq 10 |sed -n '1~2p' 1 3 5 7 9

[root@linux-node1 ~]# tail /etc/services |sed -n '$p' matahari 49000/tcp # Matahari Broker

[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

[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

[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

[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
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

[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

[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

[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; }
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

[root@linux-node1 ~]# tail /etc/services |sed -n 's/^blp5/test/p' test 48129/tcp # Bloomberg locator test 48129/udp # Bloomberg locator

[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

[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

[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

[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
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

[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

[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

[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
7、文本操作

1 2 3 4 5 6 7 8 9

[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

[root@linux-node1 ~]# tail /etc/services |sed '/blp5/w b.txt' #只有下面兩行被寫入 blp5 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator
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}' # 打印第一列的數據

[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}' # 打印第一列和第七列數據,並以制表符分隔

[root@linux-node1 aaa]# awk -F ':' '/root/{print $1}' /etc/passwd root operator

[root@linux-node1 aaa]# w|awk 'NR==1{print $6}' 2

[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

[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

[root@linux-node1 aaa]# awk '/mysql|mail/{print}' /etc/passwd mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
3、awk基本語法

[root@linux-node1 ~]# tail -n3 /etc/services |awk -F'[ /]+' '{print $2}' # 以空格或斜線分隔 48619 48619 49000

[root@linux-node1 ~]# awk -v a=123 'BEGIN{print a}' 123

[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 ===結束===
4、正則匹配

[root@linux-node1 ~]# tail /etc/services |awk '/^blp5/{print $0}' blp5 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator

[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

[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

[root@linux-node1 ~]# awk '! /^#/ && ! /^$/{print $0}' /etc/nginx/nginx.conf user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn;

[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
5、NF

[root@linux-node1 ~]# echo "a b c d e f" |awk '{print NF}' 6

[root@linux-node1 ~]# echo "a b c d e f" |awk '{print $1}' a

[root@linux-node1 ~]# echo "a b c d e f" |awk '{print $NF}' : 打印最后一行 f

[root@linux-node1 ~]# echo "a b c d e f" |awk '{print $(NF-1)}' :打印倒數第二行 e

[root@linux-node1 ~]# echo "a b c d e f" |awk '{$NF="";$(NF-1)="";print $0}' :排除最后兩個字段 a b c d
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

[root@linux-node1 ~]# tail -n5 /etc/services |awk 'NR==3{print $2}' :打印第三行第二列的值 48619/tcp

[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
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"}'

[root@linux-node1 ~]# seq 3 |awk '{print $0*2}' : 乘法 2 4 6

[root@linux-node1 ~]# seq 3 |awk '{print $0/2}' :除法 0.5 1 1.5

[root@linux-node1 ~]# seq 5 |awk '$0%2==0{print $0}' :取余 2 4

[root@linux-node1 ~]# seq 5 |shuf |awk '{print $0|"sort"}' :先打亂再排序 1 2 3 4 5
8、流程控制 (if 語句 )
if (condition) statement [ else statement ]

[root@linux-node1 ~]# seq 5 |awk '{if($1==3)print $0}' :如果第一列的值等於3,打印出來 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

[root@linux-node1 ~]# seq 5 |awk '{if($0==3)print $0;else print "no"}' :雙分支if no no 3 no no

[root@linux-node1 ~]# awk 'BEGIN{a["a"]=123}END{if("a" in a)print "yes"}' < /dev/null :判斷數組成員 yes

[root@linux-node1 ~]# awk 'BEGIN{print 1==1?"yes":"no"}' :三目運算符 yes
9、多分支if、for循環、while語句

1 2 3 4 5 6 7 8 9

[root@linux-node1 ~]# awk '{i=1;while(i<=NF){print $i;i++}}' file 1 2 3 4 5 6 7 8 9

[root@linux-node1 ~]# awk '{for(i=1;i<=NF;i++)print $i}' file 1 2 3 4 5 6 7 8 9

[root@linux-node1 ~]# awk '{for(i=NF;i>=1;i--)print $i}' file 3 2 1 6 5 4 9 8 7

[root@linux-node1 ~]# awk 'BEGIN{for(i=1;i<=5;i++){if(i==3){break};print i}}' 1 2

[root@linux-node1 ~]# awk 'BEGIN{for(i=1;i<=5;i++){if(i==3){continue};print i}}' 1 2 4 5
10、數組

[root@linux-node1 ~]# awk 'BEGIN{a[0]="test";print a[0]}' test

[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

[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

[root@linux-node1 ~]# netstat -antp |awk '/^tcp/{a[$6]++}END{for(v in a)print a[v],v}' 11 LISTEN 3 ESTABLISHED

[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