文章目錄
- 1. grep 過濾
- 2. sed 增删改查 替換
- 3. awk 擅長取列
- 4. 總結&排除空行
1. grep 過濾
grep #過濾 文本搜尋工具 給過濾的字元加上顔色 支援正則 |
選項:
-n #顯示過濾出來的内容所在檔案的行号
-v #排除,取反
-c #統計過濾出來的内容的總行數
-i #過濾的時候忽略大小寫
-o #隻顯示你要過濾的内容
-w #精确比對 隻過濾你要過濾的單詞,而不是包含這個單詞的字元串
-r #遞歸過濾 針對目錄進行操作
-A #顯示出你要過濾的内容及向下多少行的内容
-B #顯示出你要過濾的内容及向上多少行的内容
-C #顯示出你要過濾的内容向上向下各多少行
^ #以什麼開頭
$ #以什麼為結尾
-E #支援擴充正則 ==== egrep
| # 或者 擴充正則
^$ #空行 排除存在空格或者tab鍵的空行
. #任意一個字元 排除換行符
* #前面的字元出現0次或者0次以上
.* #所有
[[email protected] ~]# grep "root" passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# grep -n 'root' passwd #顯示過濾出來的内容所在檔案的行号
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# grep -v 'root' passwd #排除包含'root'内容
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
[[email protected] ~]# grep -c 'root' passwd #統計過濾出來的内容的行數
2
[[email protected] ~]# cat file.txt
root
roottt
oldboy
olirl
oldgirl
ROOT
[[email protected] ~]# grep -i 'root' file.txt #過濾内容時忽略大小寫
root
roottt
ROOT
[[email protected] ~]# grep -o 'root' passwd #隻顯示過濾的内容
root
root
root
root
[[email protected] ~]# grep 'root' file.txt
root
roottt
[[email protected] ~]# grep -w 'root' file.txt #隻過濾你要過濾的單詞而不是包含這個單詞的字元串
root
[[email protected] ~]# grep -r 'root' ./ #遞歸過濾,針對目錄的操作
./.bash_history:find root
./.bash_history:find /root/
./.bash_history:find /root -type f
./.bash_history:find /root -type f -name "^.txt"
./.bash_history:[[email protected] ~]# ll
./.bash_history:-rw-r--r--. 1 root root 38607 Jul 9 2013 7f6f8291jw1ee8c5j55rzj21hc0u0gwq.jpg
./.bash_history:-rw-r--r--. 1 root root 974 Jul 10 11:00 passwd
./.bash_history:drwxr-xr-x. 2 root root 6 Jul 10 12:04 test
./.bash_history:[[email protected] ~]#
./test.txt:root:x:0:0:root:/root:/bin/bash
./passwd:root:x:0:0:root:/root:/bin/bash
./passwd:operator:x:11:0:operator:/root:/sbin/nologin
./file.txt:root
./file.txt:roottt
[[email protected] ~]# cat file.txt
root
roottt
oldboy
olirl
oldgirl
ROOT
[[email protected] ~]# grep -A 2 'olirl' file.txt #顯示過濾的内容及向下2行的内容
olirl
oldgirl
ROOT
[[email protected] ~]# grep -B 2 'olirl' file.txt #顯示過濾的内容及向上2行的内容
roottt
oldboy
olirl
[[email protected] ~]# grep -C 2 'olirl' file.txt #顯示過濾的内容及向上向下各多少行
roottt
oldboy
olirl
oldgirl
ROOT
#過濾root或者mail的字元串 -E 支援擴充正則的使用 | 或者的意思
[[email protected] ~]# grep -E 'root|mail' passwd
root:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
rootttt
2. sed 增删改查 替換
sed #擅長增删改查 替換 後向引用
選項:
-n #取消預設輸出 和内部指令 p 組合使用 sed -n 'xxp'
-r #支援擴充正則使用 | 's###g'
-i #真正的改變檔案内容
-e #允許多項編輯
内部指令:
p #print 列印
d # 删除 排除 取反
a #追加 将内容追加指定内容的後面
i #插入 将内容插入到指定的内容的前面
s #替換
g #全局
i #忽略大小寫
\n #換行符
\t #tab鍵
= #顯示行号
#查 過濾 p
[[email protected] ~]# sed -n '/root/p' passwd #列印過濾出來的内容 取消預設輸出
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# sed -n '/^root/p' passwd #列印以root開頭的行 ^ 以什麼開頭
root:x:0:0:root:/root:/bin/bash
[[email protected] ~]# sed -nr '/root|sshd/p' passwd #列印含root和sshd的内容
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
#列印不連續的内容
[[email protected] ~]# sed -n '/sync/p;/mail/p' passwd
sync:x:5:0:sync:/sbin:/bin/sync
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
#列印某個字元串到某個字元串的所有内容
[[email protected] ~]# sed -n '/sync/,/mail/p' 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
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
#列印單行
[[email protected] ~]# sed -n '1p' passwd
root:x:0:0:root:/root:/bin/bash
#列印連續的行
[[email protected] ~]# sed -n '1,4p' 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
#列印不連續的行
[[email protected] ~]# sed -n '1p;5p' passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
#删 排除 d
[[email protected] ~]# cat -n test.txt
1 sync:x:5:0:sync:/sbin:/bin/sync
2 word
3 helld word
4 tail:root;mail;root
5 wordmagic
[[email protected] ~]# sed '/root/d' test.txt #排除包含root字元串的行
sync:x:5:0:sync:/sbin:/bin/sync
word
helld word
wordmagic
#删除不連續的字元串 删除多個字元串 以 ; 号隔開
[[email protected] ~]# sed '/root/d;/helld/d' test.txt
sync:x:5:0:sync:/sbin:/bin/sync
word
wordmagic
[[email protected] ~]# sed '/root/d;/sync/d;/helld/d' test.txt
word
wordmagic
[[email protected] ~]# sed '/root/d;1d' test.txt #删除包含root的行并删除第一行
word
helld word
wordmagic
#删除連續的字元串 以 , 隔開
[[email protected] ~]# sed '/sync/,/helld/d' test.txt
tail:root;mail;root
wordmagic
#删除單行
[[email protected] ~]# sed '1d' test.txt
word
helld word
tail:root;mail;root
wordmagic
#删除連續的行
[[email protected] ~]# sed '1,3d' test.txt
tail:root;mail;root
wordmagic
#删除不連續的行
[[email protected] ~]# sed '1d;3d' test.txt
word
tail:root;mail;root
wordmagic
#删除第一行到最後一行 $ 表示結尾
[[email protected] ~]# sed '1,$d' test.txt
#真正改變檔案的内容 選項-i #添加或删除内容不會顯示出來
[[email protected] ~]# sed -i '1d;3d' test.txt #删除第一和第三行内容
[[email protected] ~]# cat test.txt
word
tail:root;mail;root
wordmagic
[[email protected] ~]# sed '3ahello word' test.txt #追加内容到第三行後面
[[email protected] ~]# cat -n test.txt
1 word
2 tail:root;mail;root
3 wordmagic
4 hello word
[[email protected] ~]# sed -i '$ayasuo' test.txt #将内容追加至底部
[[email protected] ~]# tail -1 test.txt
yasuo
[[email protected] ~]# sed -ri '2s#(.*)#\1 zzc#g' test.txt #在行的尾巴添加内容
[[email protected] ~]# cat test.txt
word
tail:root;mail;root zzc
oldboy
wordmagic
hello word
oldgirl
yasuo
#增 a 添加内容到檔案中
#将内容追加第三行後面
[[email protected] ~]# sed '3ahello word' test.txt | cat -n
1 word
2 tail:root;mail;root
3 oldboy
4 hello word
5 wordmagic
6 hello word
7 oldgirl
8 yasuo
#将内容追加到檔案的底部 $ 表示最底部
[[email protected] ~]# sed '$aoldgirl' test.txt | cat -n
1 word
2 tail:root;mail;root
3 oldboy
4 wordmagic
5 hello word
6 oldgirl
7 yasuo
8 oldgirl
#追加多行内容到檔案中 \n 換行符 \t tab鍵
1.#将内容追加第二行後面并換行
[[email protected] ~]# sed '2adog\ncat' test.txt | cat -n # \n 換行符
1 word
2 tail:root;mail;root
3 dog
4 cat
5 oldboy
6 wordmagic
7 hello word
8 oldgirl
9 yasuo
2.#将内容追加第三行後面并加上空格
[[email protected] ~]# sed '3ayasuo\truiwen' test.txt | cat -n # \t TAB鍵
1 word
2 tail:root;mail;root
3 oldboy
4 yasuo ruiwen
5 wordmagic
6 hello word
7 oldgirl
8 yasuo
3.#根據字元串進行追加内容 追加的内容在下一行
[[email protected] ~]# sed '/oldboy/azzc' test.txt | cat -n
1 word
2 tail:root;mail;root
3 oldboy
4 zzc
5 wordmagic
6 hello word
7 oldgirl
8 yasuo
#插入 i
[[email protected] ~]# cat -n test.txt
1 word
2 tail:root;mail;root
3 oldboy
4 wordmagic
5 hello word
6 oldgirl
7 yasuo
1.#在第一行的前面插入内容
[[email protected] ~]# sed '1iruiwen' test.txt | cat -n
1 ruiwen
2 word
3 tail:root;mail;root
4 oldboy
5 wordmagic
6 hello word
7 oldgirl
8 yasuo
[[email protected] ~]# sed '1iyasuo\truiwen' test.txt | cat -n # \t TAB鍵
1 yasuo ruiwen
2 word
3 tail:root;mail;root
4 oldboy
5 wordmagic
6 hello word
7 oldgirl
8 yasuo
2.#在第三行前面插入内容
[[email protected] ~]# sed '3imimu' test.txt | cat -n
1 word
2 tail:root;mail;root
3 mimu
4 oldboy
5 wordmagic
6 hello word
7 oldgirl
8 yasuo
3.#在最後一行的前面插入内容
[[email protected] ~]# sed '$ibook' test.txt | cat -n
1 word
2 tail:root;mail;root
3 oldboy
4 wordmagic
5 hello word
6 oldgirl
7 book
8 yasuo
4.#插入多行内容
[[email protected] ~]# sed '1iruiwen\nyasuo' test.txt | cat -n # \n 換行符
1 ruiwen
2 yasuo
3 word
4 tail:root;mail;root
5 oldboy
6 wordmagic
7 hello word
8 oldgirl
9 yasuo
[[email protected] ~]# sed '1iyasuo\truiwen' test.txt | cat -n # \t TAB鍵
1 yasuo ruiwen
2 word
3 tail:root;mail;root
4 oldboy
5 wordmagic
6 hello word
7 oldgirl
8 yasuo
5.根據字元串插入内容
[[email protected] ~]# sed '/hello/izzc' test.txt | cat -n
1 word
2 tail:root;mail;root
3 oldboy
4 wordmagic
5 zzc
6 hello word
7 oldgirl
8 yasuo
#根據字元串oldboy追加内容并根據字元串hello插入内容
[[email protected] ~]# sed '/oldboy/a亞索' test.txt | sed '/hello/i你好'
word
tail:root;mail;root
oldboy
亞索
wordmagic
你好
hello word
oldgirl
yasuo
#多向編輯 -e
[[email protected] ~]# sed -e '/oldboy/a亞索' -e '/hello/i你好' test.txt
word
tail:root;mail;root
oldboy
亞索
wordmagic
你好
hello word
oldgirl
yasuo
#改 替換 's#xx#xx#g' s 替換 g 全局 i 忽略大小寫
[[email protected] ~]# cat test.txt
word
tail:root;mail;root
oldboy
wordmagic
hello word
oldgirl
yasuo
#全局替換
[[email protected] ~]# sed 's#word#zzc#g' test.txt #把所有word替換成zzc
zzc
tail:root;mail;root
oldboy
zzcmagic
hello zzc
oldgirl
yasuo
#針對某一行進行替換
#隻把第一行word替換成zzc
[[email protected] ~]# sed '1s#word#zzc#g' test.txt
zzc
tail:root;mail;root
oldboy
wordmagic
hello word
oldgirl
yasuo
#替換多行内容
#把第一行到第四行的word替換成root
[[email protected] ~]# sed '1,4s#word#root#g' test.txt
root
tail:root;mail;root
oldboy
rootmagic
hello word
oldgirl
yasuo
#替換的時候,忽略大小寫 's#xx#xx#gi'
[[email protected] ~]# echo WORD >> test.txt
[[email protected] ~]# cat test.txt
word
tail:root;mail;root
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
[[email protected] ~]# sed 's#word#zzc#gi' test.txt # i 替換時忽略大小寫
zzc
tail:root;mail;root
oldboy
zzcmagic
hello zzc
oldgirl
yasuo
zzc
#替換第五行到最後一行的内容忽略大小寫
[[email protected] ~]# cat -n test.txt
1 word
2 tail:root;mail;root
3 oldboy
4 wordmagic
5 hello word
6 oldgirl
7 yasuo
8 WORD
[[email protected] ~]# sed '5,$s#word#zzc#gi' test.txt | cat -n
1 word
2 tail:root;mail;root
3 oldboy
4 wordmagic
5 hello zzc
6 oldgirl
7 yasuo
8 zzc
#把包含root字元串行再進行替換操作
[[email protected] ~]# sed '/root/s#mail#zzc#g' test.txt
word
tail:root;zzc;root
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
#把以root開頭的行再進行替換操作
[[email protected] ~]# sed '/^root/s#bin#good#g' test.txt
word
tail:root;mail;root
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
root:x:0:0:root:/root:/good/bash
#把檔案中每個root後面加上abc
[[email protected] ~]# sed 's#root#rootabc#g' test.txt
word
tail:rootabc;mail;rootabc
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
rootabc:x:0:0:rootabc:/rootabc:/bin/bash
#給檔案的每一行前面加上注釋 #
[[email protected] ~]# sed 's#^#\##g' test.txt # \臨時把#的含義取消單純當做一個字元
#word
#tail:root;mail;root
#oldboy
#wordmagic
#hello word
#oldgirl
#yasuo
#WORD
#root:x:0:0:root:/root:/bin/bash
#把所有的#删除掉
[[email protected] ~]# sed 's#\###g' test.txt
word
tail:root;mail;root
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
root:x:0:0:root:/root:/bin/bash
#将某個字元全部删除
[[email protected] ~]# sed 's#root##g' test.txt
word
tail:;mail;
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
:x:0:0::/:/bin/bash
#排除空行 删除空行
^ #以什麼開頭
$ #以什麼為結尾
^$ #空行 排除有空格或者tab鍵的空行
[[email protected] ~]# grep -v '^$' sshd_config #排除空行
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
[[email protected] ~]# sed '/^$/d' sshd_config #删除空行
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
#後向引用 () 擴充正則
前期定義 後期調用 \1 \2 第一個括号裡面的内容 用 \1 第二個括号就是\2
[[email protected] ~]# sed -r '2s#(.*)#\1 zzc#g' test.txt
word
tail:root;mail;root zzc
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
root:x:0:0:root:/root:/bin/bash
#取IP位址
[[email protected] ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::5169:baec:f4cd:6fb9 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:02:d2:3a txqueuelen 1000 (Ethernet)
RX packets 19638 bytes 1668558 (1.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10764 bytes 1209273 (1.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[[email protected] ~]# ifconfig eth0 | sed -n '2p'
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
[[email protected] ~]# ifconfig eth0 | sed -n '2p' | sed -r 's#(^.*et)(.*)(n.*$)#\2#g'
10.0.0.100
[[email protected] ~]# ifconfig eth0 | sed -nr '2s#(^.*et)(.*)(n.*$)#\2#gp'
10.0.0.100
[[email protected] ~]# ip a s eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:02:d2:3a brd ff:ff:ff:ff:ff:ff
inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::5169:baec:f4cd:6fb9/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[[email protected] ~]# ip a s eth0 | sed -n '3p'
inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
[[email protected] ~]# ip a s eth0 | sed -n '3p' | sed -r 's#(^.*et)(.*)(/.*$)#\2#g'
10.0.0.100
[[email protected] ~]# ip a s eth0 | sed -nr '3s#(^.*et)(.*)(/.*$)#\2#gp'
10.0.0.100
#将passwd檔案中的第一列與第七列的位置進行調換
[[email protected] ~]# cat 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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
[[email protected] ~]# sed -nr 's#(^.*)(:x.*:)(.*)#\3\2\1#gp' passwd
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin
/sbin/nologin:x:2:2:daemon:/sbin:daemon
/sbin/nologin:x:3:4:adm:/var/adm:adm
/sbin/nologin:x:4:7:lp:/var/spool/lpd:lp
/bin/sync:x:5:0:sync:/sbin:sync
/sbin/shutdown:x:6:0:shutdown:/sbin:shutdown
/sbin/halt:x:7:0:halt:/sbin:halt
/sbin/nologin:x:8:12:mail:/var/spool/mail:mail
/sbin/nologin:x:11:0:operator:/root:operator
/sbin/nologin:x:12:100:games:/usr/games:games
/sbin/nologin:x:14:50:FTP User:/var/ftp:ftp
/sbin/nologin:x:99:99:Nobody:/:nobody
#列印行号
[[email protected] ~]# sed = test.txt
1
word
2
tail:root;mail;root zzc
3
oldboy
4
wordmagic
5
hello word
6
oldgirl
7
yasuo
3. awk 擅長取列
awk #擅長取列 計算 數組 函數 一種程式設計語言
選項:
-F #指定分隔符 預設以空白字元為分隔符
-v #指定内部變量
内部指令 内部變量
Fs #指定輸入分隔符
OFS #指定輸出分隔符
NR #行号
NF #最後一列 為第幾列
$0 #完整的一行内容
$n # n 是數字 表示取出第幾列 多列用逗号分割
$NF #顯示最後一列的内容
#查 過濾
[[email protected] ~]# awk '/root/' passwd #過濾出包含root的内容
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# awk '/root/;/adm/' passwd #過濾出包含root和adm的内容
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# awk '/root|adm/' passwd #過濾出包含root和adm的内容
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# awk '/adm/,/mail/' passwd #過濾出包含adm到mail的内容
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
#取行
[[email protected] ~]# awk 'NR==1' passwd #取第一行
root:x:0:0:root:/root:/bin/bash
[[email protected] ~]# awk 'NR==1,NR==3' 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
[[email protected] ~]# awk 'NR==1;NR==3' passwd #取第一行和第三行
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[[email protected] ~]# cat -n test.txt
1 word
2 tail:root;mail;root zzc
3 oldboy
4 wordmagic
5 hello word
6 oldgirl
7 yasuo
8 WORD
9 root:x:0:0:root:/root:/bin/bash
[[email protected] ~]# awk 'NR>4' test.txt #取第四行後面的内容不包含第四行
hello word
oldgirl
yasuo
WORD
root:x:0:0:root:/root:/bin/bash
[[email protected] ~]# awk 'NR<4' test.txt #取第四行前面的内容不包含第四行
word
tail:root;mail;root zzc
oldboy
[[email protected] ~]# awk 'NR<=4' test.txt #取包含第四行前面的内容
word
tail:root;mail;root zzc
oldboy
wordmagic
[[email protected] ~]# awk 'NR>=4' test.txt #取包含第四行很後面的内容
wordmagic
hello word
oldgirl
yasuo
WORD
root:x:0:0:root:/root:/bin/bash
#&& 并且
[[email protected] ~]# awk 'NR>2 && NR<5' test.txt #取第三行到第四行的内容
oldboy
wordmagic
#|| 或者
[[email protected] ~]# awk 'NR<2 || NR>6' test.txt #取小于第二行或取大于第六行的内容
word
yasuo
WORD
root:x:0:0:root:/root:/bin/bash
#列印整個檔案内容 {print $0}
[[email protected] ~]# awk '{print $0}' test.txt
word
tail:root;mail;root zzc
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
root:x:0:0:root:/root:/bin/bash
#給檔案内容加上行号
[[email protected] ~]# awk '{print NR,$0}' test.txt
1 word
2 tail:root;mail;root zzc
3 oldboy
4 wordmagic
5 hello word
6 oldgirl
7 yasuo
8 WORD
9 root:x:0:0:root:/root:/bin/bash
#取反 排除 !
[[email protected] ~]# awk '!/root/' test.txt #排除包含root的内容
word
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
[[email protected] ~]# awk 'NR!=1' test.txt #排除第一行内容
tail:root;mail;root zzc
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
root:x:0:0:root:/root:/bin/bash
# 取列 -F #分隔符的變量 $n # n 是數字 表示取出第幾列 多列用逗号分割
#以 :為分隔符取第一列内容
[[email protected] ~]# awk -F '[:]' '{print $1}' passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
#以 :為分隔符取第一列和第二列内容
[[email protected] ~]# awk -F '[:]' '{print $1,$2}' passwd
root x
bin x
daemon x
adm x
lp x
sync x
shutdown x
halt x
mail x
operator x
games x
ftp x
nobody x
systemd-network x
dbus x
polkitd x
sshd x
postfix x
#以 :為分隔符取最後一列内容 $NF #最後一列
[[email protected] ~]# awk -F '[:]' '{print $NF}' passwd
/bin/bash
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/bin/sync
/sbin/shutdown
/sbin/halt
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
# $(NF-1) 倒數第二列
[[email protected] ~]# awk -F '[:]' '{print $(NF-1)}' passwd
/root
/bin
/sbin
/var/adm
/var/spool/lpd
/sbin
/sbin
/sbin
/var/spool/mail
/root
/usr/games
/var/ftp
/
/
/
/
/var/empty/sshd
/var/spool/postfix
#預設分隔符為 空白字元
[[email protected] ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::3310:9d15:9ee4:43e8 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:eb:ea:8d txqueuelen 1000 (Ethernet)
RX packets 15508 bytes 1698801 (1.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10471 bytes 1145384 (1.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[[email protected] ~]# ifconfig eth0 | awk 'NR==2'
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
[[email protected] ~]# ifconfig eth0 | awk 'NR==2' | awk '{print $2}'
10.0.0.100
[[email protected] ~]# ifconfig eth0 | awk 'NR==2{print $2}'
10.0.0.100
#指定多個分隔符
[[email protected] ~]# ip a s eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:eb:ea:8d brd ff:ff:ff:ff:ff:ff
inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::3310:9d15:9ee4:43e8/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[[email protected] ~]# ip a s eth0 | awk 'NR==3'
inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
[[email protected] ~]# ip a s eth0 | awk 'NR==3' | awk -F '[ /]' '{print $6}'
10.0.0.100
[[email protected] ~]# ip a s eth0 | awk 'NR==3' | awk -F '[ /]*' '{print $3}'
10.0.0.100
[[email protected] ~]# echo " // inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0" >ip.txt
[[email protected] ~]# cat ip.txt
// inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
[[email protected] ~]# awk -F '[ /]' '{print $11}' ip.txt
10.0.0.100
[[email protected] ~]# awk -F '[ /]*' '{print $3}' ip.txt
10.0.0.100
#指定分隔符 使用雙引号引起來 "[]"
[[email protected] ~]# awk -F: '{print $1":"$2}' passwd
root:x
bin:x
daemon:x
adm:x
lp:x
sync:x
shutdown:x
halt:x
mail:x
operator:x
games:x
ftp:x
nobody:x
systemd-network:x
dbus:x
polkitd:x
sshd:x
postfix:x
[[email protected] ~]# awk -F: '{print $1"脫産10期"$2}' passwd
root脫産10期x
bin脫産10期x
daemon脫産10期x
adm脫産10期x
lp脫産10期x
sync脫産10期x
shutdown脫産10期x
halt脫産10期x
mail脫産10期x
operator脫産10期x
games脫産10期x
ftp脫産10期x
nobody脫産10期x
systemd-network脫産10期x
dbus脫産10期x
polkitd脫産10期x
sshd脫産10期x
postfix脫産10期x
[[email protected] ~]# awk -F: -vOFS=":" '{a=$1;$1=$NF;$NF=a;print}' passwd
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin
/sbin/nologin:x:2:2:daemon:/sbin:daemon
rootroot
/sbin/nologin:x:3:4:adm:/var/adm:adm
/sbin/nologin:x:4:7:lp:/var/spool/lpd:lp
/bin/sync:x:5:0:sync:/sbin:sync
/sbin/shutdown:x:6:0:shutdown:/sbin:shutdown
/sbin/halt:x:7:0:halt:/sbin:halt
/sbin/nologin:x:8:12:mail:/var/spool/mail:mail
/sbin/nologin:x:11:0:operator:/root:operator
/sbin/nologin:x:12:100:games:/usr/games:games
/sbin/nologin:x:14:50:FTP User:/var/ftp:ftp
/sbin/nologin:x:99:99:Nobody:/:nobody
/sbin/nologin:x:192:192:systemd Network Management:/:systemd-network
/sbin/nologin:x:81:81:System message bus:/:dbus
/sbin/nologin:x:999:998:User for polkitd:/:polkitd
/sbin/nologin:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:tss
/sbin/nologin:x:173:173::/etc/abrt:abrt
/sbin/nologin:x:74:74:Privilege-separated SSH:/var/empty/sshd:sshd
/sbin/nologin:x:89:89::/var/spool/postfix:postfix
/sbin/nologin:x:38:38::/etc/ntp:ntp
/bin/bash:x:1000:1000::/home/zzc:zzc
4. 總結&排除空行
1. grep #過濾 給過濾出來的内容加上顔色
選項:
-n #給過濾出來的内容加上此行所在檔案的行号
-i #忽略大小寫
-c #統計的是行數 包含過濾字元串的行數
-v #排除 删除 取反
-o #隻顯示過濾出來的内容
-w #精确比對 隻比對你要過濾的字元串,而不是過濾包含此字元串的字元串
-r #遞歸過濾 過濾多個檔案 針對目錄操作
-A #比對過濾出來的内容向下多少行 後面加正整數
-B #比對過濾出來的内容向上多少行
-C #比對過濾出來的内容各向上向下多少行
-E #支援擴充正則使用 === egrep
^ #以什麼開頭
$ #以什麼為結尾
^$ #空行 排除存在空格或者tab鍵的空行
. #比對除換行符以外的任意一個字元
* #比對前面的字元出現0次或者0次以上
.* #所有
| #或者 擴充正則
2. sed #擅長替換 增删改查 後向引用
選項:
-n #取消預設輸出
-r #支援擴充正則
-i #真正的改變檔案内容
-e #允許多項編輯 了解
内部指令:
p #列印
d #删除 排除 取反
s #替換
g #全局
i #忽略大小寫
a #追加
i #插入
\n #換行符
\t #tab鍵
= #顯示行号
後向引用:
[[email protected] ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::5169:baec:f4cd:6fb9 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:02:d2:3a txqueuelen 1000 (Ethernet)
RX packets 194 bytes 17674 (17.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 100 bytes 10541 (10.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[[email protected] ~]# ifconfig eth0 | sed -n '2p' | sed -r 's#(.*et )(.*)( n.*)#\2#g'
10.0.0.100
[[email protected] ~]# ifconfig eth0 | sed -nr '2s#(.*et )(.*)( n.*)#\2#gp'
10.0.0.100
[[email protected] ~]# ip a s eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:02:d2:3a brd ff:ff:ff:ff:ff:ff
inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::5169:baec:f4cd:6fb9/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[[email protected] ~]# ip a s eth0 | sed -n '3p' | sed -r 's#(.*et )(.*)(/.*)#\2#g'
10.0.0.100
[[email protected] ~]# ip a s eth0 | sed -nr '3s#(.*et )(.*)(/.*)#\2#gp'
10.0.0.100
[[email protected] ~]# stat /etc/hosts
File: ‘/etc/hosts’
Size: 158 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 17004494 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:net_conf_t:s0
Access: 2020-07-15 09:49:48.356726160 +0800
Modify: 2013-06-07 22:31:32.000000000 +0800
Change: 2020-07-06 09:29:45.882290124 +0800
Birth: -
[[email protected] ~]# stat /etc/hosts | sed -nr '4s#(.*\()(.*)(/-.*)#\2#gp'
0644
3. awk #擅長取列
選項:
-F #指定分隔符 預設的為空白字元為分隔符 FS
-v #指定内部變量
! #取反 排除
内部變量:
FS #指定輸入分隔符
OFS #指定輸出分隔符
NR #行号
NF #最後一列
$NF #顯示最後一列
$0 #完整的一行内容
$n #n數字 指定取出的某一列
1. 排除空行 存在空格和tab鍵的空行
[[email protected] ~]# cat sshd_config
penBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
1.# ^$ 空行 \s 空白字元 \t tab鍵
[[email protected] ~]# grep -v '^$' sshd_config
[[email protected] ~]# sed '/^$/d' sshd_config
[[email protected] ~]# awk '!/^$/' sshd_config
# \t tab鍵
[[email protected] ~]# grep -Pv '^[ \t]*$' sshd_config # -P 支援tab鍵
[[email protected] ~]# sed '/^[ \t]*$/d' sshd_config
[[email protected] ~]# awk '!/^[ \t]*$/' sshd_config
# \s 空白字元
[[email protected] ~]# grep -v '^\s*$' sshd_config
[[email protected] ~]# sed '/^\s*$/d' sshd_config
[[email protected] ~]# awk '!/^\s*$/' sshd_config
2.#排除空行和注釋行 # 開頭
[[email protected] ~]# grep -Ev '^$|^#' sshd_config
penBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
[[email protected] ~]# grep -Ev '^\s*$|^#' sshd_config
penBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
[[email protected] ~]# sed -r '/^[ \t]*$|^#/d' sshd_config
penBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
[[email protected] ~]# sed -r '/^\s*$|^#/d' sshd_config
penBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
[[email protected] ~]# awk '!/^\s*$|^#/' sshd_config
penBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
[[email protected] ~]# awk '!/^[ \t]*$|^#/' sshd_config
penBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
3.#将/etc/passwd檔案中的第一列和第七列位置進行調換 awk 等值替換
[[email protected] ~]# awk -F '[:]' '{a=$1;$1=$NF;$NF=a;print}' passwd | tr ' ' ':'
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin
/sbin/nologin:x:2:2:daemon:/sbin:daemon
/sbin/nologin:x:3:4:adm:/var/adm:adm
/sbin/nologin:x:4:7:lp:/var/spool/lpd:lp
/bin/sync:x:5:0:sync:/sbin:sync
/sbin/shutdown:x:6:0:shutdown:/sbin:shutdown
/sbin/halt:x:7:0:halt:/sbin:halt
/sbin/nologin:x:8:12:mail:/var/spool/mail:mail
/sbin/nologin:x:11:0:operator:/root:operator
/sbin/nologin:x:12:100:games:/usr/games:games
/sbin/nologin:x:14:50:FTP:User:/var/ftp:ftp
/sbin/nologin:x:99:99:Nobody:/:nobody
/sbin/nologin:x:192:192:systemd:Network:Management:/:systemd-network
/sbin/nologin:x:81:81:System:message:bus:/:dbus
/sbin/nologin:x:999:998:User:for:polkitd:/:polkitd
[[email protected] ~]# awk -F '[:]' -vOFS=":" '{a=$1;$1=$NF;$NF=a;print}' passwd
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin
/sbin/nologin:x:2:2:daemon:/sbin:daemon
/sbin/nologin:x:3:4:adm:/var/adm:adm
/sbin/nologin:x:4:7:lp:/var/spool/lpd:lp