一、cut 命令
-d 指定分隔符爲:顯示1-3列: cut -d : -f 1-3 /etc/passwd
-c 顯示第一和第四個字符: -cut -c 1,4 /etc/passwd
-f 列
cut練習1:獲取主機ip:
方法一:ifconfig eth0 | grep "inet " | cut -d " " -f 10
ifconfig eth0 | grep "inet " | cut -d " " -f 10
過程分步:
1.[root@server6 ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.17.6 netmask 255.255.255.0 broadcast 172.25.17.255
inet6 fe80::5054:ff:fef8:b5f9 prefixlen 64 scopeid 0x20<link>
ether 52:54:00:f8:b5:f9 txqueuelen 1000 (Ethernet)
RX packets 81 bytes 9141 (8.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 190 bytes 13695 (13.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2.[root@server6 ~]# ifconfig eth0 | grep "inet"
inet 172.25.17.6 netmask 255.255.255.0 broadcast 172.25.17.255
inet6 fe80::5054:ff:fef8:b5f9 prefixlen 64 scopeid 0x20<link>
[root@server6 ~]# ifconfig eth0 | grep "inet "
inet 172.25.17.6 netmask 255.255.255.0 broadcast 172.25.17.255
3.[root@server6 ~]# ifconfig eth0 | grep "inet "|cut -d " " -f 10
172.25.17.6
方法二:ifconfig eth0 | grep "inet " | awk '{print $2}'
[root@server6 ~]# ifconfig eth0 | grep "inet "|awk '{print $2}'
172.25.17.6
練習2:檢測網絡
ping -c1 -w1 172.25.254.$1 &> /dev/null && echo 172.25.254.$1 is up || echo 172.25.254.$1 is down
練習3:查看cpu前五進程
[root@localhost Desktop]# ps -ax -o %cpu,pid --sort=-%cpu | grep -v PID | head -n 5
0.0 1
0.0 2
0.0 3
0.0 5
0.0 7
二、sort 命令
sort -n 純數字排序
-r 倒敘
-u 去掉重複數字
-o 輸出到指定文件中
-t 指定分隔符
-k 指定要排序的列
【默認排序以第一個數字大小進行排序】
[root@server ~]# sort westos
1
12
123
2
3
32
5
51
6
7
sort -n 純數字排序
[root@server ~]# sort -n westos
1
2
3
5
6
7
12
32
51
123
sort -t : -k 2 westos #指定以冒號爲分隔符,對第二列排序
[root@server ~]# sort -t : -k 2 westos
2:0
12:10
2:12
3:2
51:20
5:21
123:22
32:31
5:4
6:4
1:5
51:55
123:66
7:79
三、uniq命令---對重複字符處理
uniq
-u 顯示唯一的行
-d 顯示重複的行
-c 每行顯示一次並統計重複次數
sort -n westos | uniq -d #先對westos進行排序,再顯示重複的行
[root@server ~]# sort -n westos | uniq -d
2
10
31
sort -n westos | uniq -u #先對westos進行排序,再顯示唯一的行
[root@server ~]# sort -n westos | uniq -u
0
1
4
6
9
20
22
55
[root@localhost Desktop]# sort -n 2.sh | uniq -u -c
1 5
1 7
1 8
1 33
1 34
1 45
1 65
1 67
練習:導出 /tmp下最大的文件,並輸出文件名
方法一:
[root@localhost kiosk]# ls -Sl /tmp/ | head -2 | cut -d " " -f 13
yum_save_tx.2019-04-04.21-50.QDEKFf.yumtx
方法二:
[root@localhost kiosk]# ls -Sl /tmp/ |head -2 |awk '{print $9}'
yum_save_tx.2019-04-04.21-50.QDEKFf.yumtx
四、test命令
test 1==2 等同於 [ 1 == 2 ] 等同於 [ 1 - eq 2]
test "$a" == "$b" 等同於 [ "a" == "$b" ]
[ "$a" = "$b" ] 等於
[ "$a" != "$b" ] 不等於
[ "$a" - eq "$b" ] "等於
[ "$a" - ne "$b" ] 不等於
[ "$a" - le "$b" ] 小於等於
[ "$a" - ge "$b" ] 大於等於
[ "$a" - gt "$b" ] 大於
[ "$a" - lt "$b" ] 小於
[ "$a" - ne "$b" -a "$a" -gt "$b" ] -a必須條件都滿足
[ "$a" - ne "$b" -o "$a" -gt "$b" ] -a條件至少滿足一個
[ -z "$a" ] 是否爲空
[ -e "file" ] 是否存在
[ -f "file" ] 普通文件
[ -b "file" ] 塊設備
[ -S "file" ] 套接字
[ -c "file" ] 字符設備
[ -L "file" ] 軟連接
練習:用戶輸入一個數,是否在10以內
思路:1.判斷是否爲空
2.是否在10以內
3,1<$a<10 -- > yes
4.$a>10 ---- > no
#判斷輸入的數字是否在10以內
#1.輸入是否爲空
#2.是否在10以內
#3.1<$a<10 --> yes
#4.$a<1 $a>10 --> no
#!/bin/bash
[ -z "$1" ] && {
echo "please input a number!"
exit 1
}
[ "$1" -gt "0" -a "$1" -lt "10" ] && {
echo "YES"
}||{
echo "NO"
}
五、sed命令---stream editor :一次處理一行內容,處理時,把當前的行存儲在臨時緩衝區,處理完後,輸送到屏幕
sed [參數] '命令' file
p ##顯示
d ##刪除
a ##添加
c ##替換
i ##插入
1) p:顯示
sed -n '/^#/p' /etc/fstab 顯示以#開頭
sed -n '/\:/p' /etc/fstab 有冒號的行,冒號需要轉義
sed -n '/\:/!p' /etc/fstab 沒有冒號的行,冒號需要轉義
sed -n '2,6p 顯示2-6行
sed -n '2,6!p 不顯示2-6行
sed 's/\//#/g' /etc/fstab 將/etc/fstab中的所有/換爲# /需要轉義
(注意:不加-i 只展示出效果,真正文件並不替換)
2) d:刪除
刪除以UID 開頭的:sed '/^UUID/d' /etc/fstab
刪除空行:sed '/^$/d' /etc/fstab
刪除指定行: sed '1,4d' /etc/fstab
3)a:添加
換行:sed '/hello/aworld' westos
不換行:sed 's/hello/hello world/g' westos
換行:sed 's/hello/hello\nworld/g' westo
4)c:替換
sed '/hello/chello world' westos
(不加-i 只展示出效果,真正文件並不替換)
5)i 插入:
sed -i 's/\//#/g' /etc/fstab 將/etc/fstab中的所有/換爲# /需要轉義
(不加-i 只展示出效果,真正文件並不替換)
#s/表示開頭
#/g表示全局
六、awk命令;
awk處理機制:一行一行的讀,根據模式一次從文件中抽取i一行文本,對這行文本進行切片,默認使用空白字符作爲分隔符
awk '{print FILENAME,NR}' /etc/passwd 輸出文件名以及行號
awk -F: '{print NR,NF}' /etc/passwd 輸出每次處理的行號以及當前以冒號爲分隔符的字段個數
[root@server mnt]# cat test
this | is | a | file
$1 $2 $3 $4
awk '{print $1}' test
this
awk '{print $4}' test
file
awk '{print $1,$2}' test 顯示兩個
this is
0代表一整行
BEGIN {}讀入第一行文本之前執行的語句,一般用來初始化操作
{}:逐行處理
END{} 處理完最後一行文本後執行,一般用來處理輸出結果
awk 'BEGIN { a=34;print a+10 }'
第一行之前加REDHAT 末尾加WESTOS 打印行號和內容,以冒號爲分隔符:
awk -F: 'BEGIN{print "REDHAT"} {print NR;print } END {print "WESTOS"}' passwd
1)輸出以bash結尾的:
輸出以bash結尾的:
awk -F: '/bash$/{print}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
student:x:1000:1000:Student User:/home/student:/bin/bash
2):輸出第三行:
awk -F: 'NR==3 {print}' /etc/passwd
3):輸出偶數行:
awk -F: 'NR % 2 == 0 {print}' /etc/passwd
4):輸出前三行:
awk -F: 'NR <= 3 {print}' /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
練習1:找出/etc/passwd系統中uid<2用戶的uid以及用戶名 打印
[root@localhost kiosk]# awk -F: '$3 < 2 {print $1,$3}' /etc/passwd
root 0
bin 1
練習2:統計文本總字段個數:
[root@localhost kiosk]# awk 'BEGIN{i=0}{i+=NF}END{print i}' test.txt
5
[root@localhost kiosk]# cat test.txt
ABSD XX
XX
ABSDABCD XX
練習3:列出文件的3-5行;
awk -F: 'NR >=3 && NR <=5 {print }' /etc/passwd