Shell工具 【超重點(diǎn)】
cut
cut的工作就是“剪”幢痘,具體的說就是在文件中負(fù)責(zé)剪切數(shù)據(jù)用的。cut 命令從文件的每一行剪切字節(jié)、字符和字段并將這些字節(jié)悯辙、字符和字段輸出。
1.基本用法
cut [選項(xiàng)參數(shù)] filename
說明:默認(rèn)分隔符是制表符
2.選項(xiàng)參數(shù)說明(常用)
選項(xiàng)參數(shù) 功能
-f 列號迎吵,提取第幾列
-d 分隔符躲撰,按照指定分隔符分割列
3.案例實(shí)操
[root@master shell]# cat cu.txt
dong shen
guan zhen
wo wo
lai lai
le le
[root@master shell]# cut -d " " -f 1 cu.txt //取出第一列,按空格分
dong
guan
wo
lai
le
[root@master shell]# cut -d " " -f 1,2 cu.txt //取出第一击费、列
dong shen
guan zhen
wo
lai
le
[root@master shell]# cat cu.txt | grep guan //取出 guan所在行
guan zhen
[root@master shell]# cat cu.txt | grep guan | cut -d " " -f 1 //取出guan
guan
選取系統(tǒng)PATH變量值拢蛋,分別切出第2個(gè)“:”開始后和之前的所有路徑
切割ifconfig 后打印的IP地址
先限定在“eth0”范圍;再選定為“inet addr”行蔫巩;選定行以“:”為分隔符進(jìn)行切分谆棱,并選出第二列;再以“ ”為分隔符圆仔,并選定第一列
sed 先緩存再處理
[root@master shell]# cat sed.txt
dong shen
guan zhen
wo wo
lai lai
le le
[root@master shell]# sed "2a mei nv" sed.txt //將“mei nv”這個(gè)單詞插入到sed.txt第二行下垃瞧,打印
dong shen
guan zhen
mei nv
wo wo
lai lai
le le
[root@master shell]# cat sed.txt //并不會(huì)改變源文件
dong shen
guan zhen
wo wo
lai lai
le le
[root@master shell]# sed "/wo/d" sed.txt //刪除sed.txt文件所有包含wo的行
dong shen
guan zhen
lai lai
le le
[root@master shell]# sed "s/wo/ni/g" sed.txt //刪除sed.txt文件所有包含wo的行;‘g’表示global坪郭,全部替換
dong shen
guan zhen
ni ni
lai lai
le le
[root@master shell]# sed -e "2d" -e "s/wo/ni/g" sed.txt //將sed.txt文件中的第二行刪除并將wo替換為ni
dong shen
ni ni
lai lai
le le
awk 重點(diǎn)
一個(gè)強(qiáng)大的文本分析工具个从,把文件逐行的讀入,以空格為默認(rèn)分隔符將每行切片,切開的部分再進(jìn)行分析處理嗦锐。
基本用法
awk [選項(xiàng)參數(shù)] ‘pattern1{action1} pattern2{action2}...’ filename
pattern:表示AWK在數(shù)據(jù)中查找的內(nèi)容嫌松,就是匹配模式
action:在找到匹配內(nèi)容時(shí)所執(zhí)行的一系列命令選項(xiàng)參數(shù)說明
選項(xiàng)參數(shù) 功能
-F 指定輸入文件折分隔符
-v 賦值一個(gè)用戶定義變量案例實(shí)操
(1)搜索passwd文件以root關(guān)鍵字開頭的所有行,并輸出該行的第7列;
"-F :" 以":"進(jìn)行分割意推,"^root"行開頭豆瘫,的第7列 ,文件名
[root@master shell]# awk -F : '/^root/ {print $7}' passwd
/bin/bash
(2)搜索passwd文件以root關(guān)鍵字開頭的所有行菊值,并輸出該行的第1列和第7列外驱,中間以“%”號分割
[root@master shell]# awk -F : '/^root/ {print $1,$7}' passwd
root /bin/bash
[root@master shell]# awk -F : '/^root/ {print $1"%",$7}' passwd
root% /bin/bash
(3)只顯示/etc/passwd的第一列和第七列,以逗號分割腻窒,且在所有行前面添加列名user昵宇,shell在最后一行添加"long, bin/love"。
BEGIN 在所有數(shù)據(jù)讀取行之前執(zhí)行儿子;END 在所有數(shù)據(jù)執(zhí)行之后執(zhí)行
(4)將passwd文件中的用戶id增加數(shù)值1并輸出
- awk的內(nèi)置變量
變量 說明
FILENAME 文件名
NR 已讀的記錄數(shù)
NF 瀏覽記錄的域的個(gè)數(shù)(切割后瓦哎,列的個(gè)數(shù)) - 案例實(shí)操
(1)統(tǒng)計(jì)passwd文件名,每行的行號柔逼,每行的列數(shù)
[root@master shell]# awk -F : '{print FILENAME"," NR"," NF}' passwd
passwd,1,7
passwd,2,7
passwd,3,7
passwd,4,7
passwd,5,7
passwd,6,7
passwd,7,7
passwd,8,7
passwd,9,7
passwd,10,7
passwd,11,7
passwd,12,7
passwd,13,7
passwd,14,7
(2)切割I(lǐng)P
[root@master shell]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:2B:B8:C5
inet addr:192.168.31.160 Bcast:192.168.31.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe2b:b8c5/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:22066 errors:0 dropped:0 overruns:0 frame:0
TX packets:3851 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:5485323 (5.2 MiB) TX bytes:594835 (580.8 KiB)
[root@master shell]# ifconfig eth0 | grep "inet addr"
inet addr:192.168.31.160 Bcast:192.168.31.255 Mask:255.255.255.0
[root@master shell]# ifconfig eth0 | grep "inet addr" |awk -F : '{print $2}'
192.168.31.160 Bcast
[root@master shell]# ifconfig eth0 | grep "inet addr" |awk -F : '{print $2}' | awk -F " " '{print $1}'
192.168.31.160
(3)查詢sed.txt中空行所在的行號
[root@master shell]# awk '/^$/ {print NR}' sed.txt // "/^s/"指空行
5
7
sort
- 案例實(shí)操
[root@master shell]# cat sort.sh
bb:40:5.4
bd:20:4.2
xz:50:2.3
cls:10:3.5
ss:30:1.6
[root@master shell]# sort -t : -nrk 2 sort.sh *按照“:”分割后的第二列倒序排序
xz:50:2.3
bb:40:5.4
ss:30:1.6
bd:20:4.2
cls:10:3.5
綜合練習(xí)
1.使用Linux命令計(jì)算第二列的和并輸出
[root@master shell]# cat cheng.txt
mike 40
huhau 90
liu 20
[root@master shell]# cat cheng.txt | awk -F " " '{sum+=$2} END{print sum}'
150
2.Shell腳本里如何檢查一個(gè)文件是否存在蒋譬?如果不存在該如何處理?
#!/bin/bash
if [ -e file.txt ]; then
echo "文件存在!"
else
echo "文件不存在!"
fi
3.用shell寫一個(gè)腳本愉适,對文本中無序的一列數(shù)字排序犯助,并求和
root@master shell]# cat test.txt
9
8
7
6
5
4
3
1
10
1
[root@master shell]# sort -n test.txt
1
1
3
4
5
6
7
8
9
10
[root@master shell]# cat test.txt | awk '{sum+=$1;print$1} END{print "sum="sum}'
9
8
7
6
5
4
3
1
10
1
sum=54
4.請用shell腳本寫出查找當(dāng)前文件夾(/home)下所有的文本文件內(nèi)容中包含有字符”shen”的文件名稱
[root@master shell]# grep -r "shen" ./
./sed.txt:dong shen
./cu.txt:dong shen
[root@master shell]# grep -r "shen" ./ | cut -d ":" -f 1
./sed.txt
./cu.txt