一、 cut
cut的工作就是“剪”,具體的說就是在文件中負責剪切數據用的。cut 命令從文件的每一行剪切字節、字符和字段並將這些字節、字符和字段輸出。
1.基本用法
cut [選項參數] filename
說明:默認分隔符是制表符
2.選項參數說明
表1-55
選項參數 |
功能 |
-f |
列號,提取第幾列 |
-d |
分隔符,按照指定分隔符分割列 |
-c |
指定具體的字符 |
3.案例實操
(0)數據准備
[atguigu@hadoop101 datas]$ touch cut.txt
[atguigu@hadoop101 datas]$ vim cut.txt
dong shen
guan zhen
wo wo
lai lai
le le
(1)切割cut.txt第一列
[atguigu@hadoop101 datas]$ cut -d " " -f 1 cut.txt
dong
guan
wo
lai
le
(2)切割cut.txt第二、三列
[atguigu@hadoop101 datas]$ cut -d " " -f 2,3 cut.txt
shen
zhen
wo
lai
le
(3)在cut.txt文件中切割出guan
[atguigu@hadoop101 datas]$ cat cut.txt | grep "guan" | cut -d " " -f 1
guan
(4)選取系統PATH變量值,第2個“:”開始后的所有路徑:
[atguigu@hadoop101 datas]$ echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/atguigu/bin
[atguigu@hadoop102 datas]$ echo $PATH | cut -d: -f 2-
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/atguigu/bin
(5)切割ifconfig 后打印的IP地址
[atguigu@hadoop101 datas]$ ifconfig eth0 | grep "inet addr" | cut -d: -f 2 | cut -d" " -f1
192.168.1.102
二、sed
sed是一種流編輯器,它一次處理一行內容。處理時,把當前處理的行存儲在臨時緩沖區中,稱為“模式空間”,接着用sed命令處理緩沖區中的內容,處理完成后,把緩沖區的內容送往屏幕。接着處理下一行,這樣不斷重復,直到文件末尾。文件內容並沒有改變,除非你使用重定向存儲輸出。
- 基本用法
sed [選項參數] ‘command’ filename
- 選項參數說明
表1-56
選項參數 |
功能 |
-e |
直接在指令列模式上進行sed的動作編輯。 |
-i |
直接編輯文件 |
- 命令功能描述
表1-57
命令 |
功能描述 |
a |
新增,a的后面可以接字串,在下一行出現 |
d |
刪除 |
s |
查找並替換 |
- 案例實操
(0)數據准備
[atguigu@hadoop102 datas]$ touch sed.txt
[atguigu@hadoop102 datas]$ vim sed.txt
dong shen
guan zhen
wo wo
lai lai
le le
(1)將“mei nv”這個單詞插入到sed.txt第二行下,打印。
[atguigu@hadoop102 datas]$ sed '2a mei nv' sed.txt
dong shen
guan zhen
mei nv
wo wo
lai lai
le le
[atguigu@hadoop102 datas]$ cat sed.txt
dong shen
guan zhen
wo wo
lai lai
le le
注意:文件並沒有改變
(2)刪除sed.txt文件所有包含wo的行
[atguigu@hadoop102 datas]$ sed '/wo/d' sed.txt
dong shen
guan zhen
lai lai
le le
(3)將sed.txt文件中wo替換為ni
[atguigu@hadoop102 datas]$ sed 's/wo/ni/g' sed.txt
dong shen
guan zhen
ni ni
lai lai
le le
注意:‘g’表示global,全部替換
(4)將sed.txt文件中的第二行刪除並將wo替換為ni
[atguigu@hadoop102 datas]$ sed -e '2d' -e 's/wo/ni/g' sed.txt
dong shen
ni ni
lai lai
le le
三、 awk
一個強大的文本分析工具,把文件逐行的讀入,以空格為默認分隔符將每行切片,切開的部分再進行分析處理。
- 基本用法
awk [選項參數] ‘pattern1{action1} pattern2{action2}...’ filename
pattern:表示AWK在數據中查找的內容,就是匹配模式
action:在找到匹配內容時所執行的一系列命令
- 選項參數說明
表1-55
選項參數 |
功能 |
-F |
指定輸入文件折分隔符 |
-v |
賦值一個用戶定義變量 |
- 案例實操
(0)數據准備
[atguigu@hadoop102 datas]$ sudo cp /etc/passwd ./
(1)搜索passwd文件以root關鍵字開頭的所有行,並輸出該行的第7列。
[atguigu@hadoop102 datas]$ awk -F: '/^root/{print $7}' passwd
/bin/bash
(2)搜索passwd文件以root關鍵字開頭的所有行,並輸出該行的第1列和第7列,中間以“,”號分割。
[atguigu@hadoop102 datas]$ awk -F: '/^root/{print $1","$7}' passwd
root,/bin/bash
注意:只有匹配了pattern的行才會執行action
(3)只顯示/etc/passwd的第一列和第七列,以逗號分割,且在所有行前面添加列名user,shell在最后一行添加"dahaige,/bin/zuishuai"。
[atguigu@hadoop102 datas]$ awk -F : 'BEGIN{print "user, shell"} {print $1","$7} END{print "dahaige,/bin/zuishuai"}' passwd
user, shell
root,/bin/bash
bin,/sbin/nologin
。。。
atguigu,/bin/bash
dahaige,/bin/zuishuai
注意:BEGIN 在所有數據讀取行之前執行;END 在所有數據執行之后執行。
(4)將passwd文件中的用戶id增加數值1並輸出
[atguigu@hadoop102 datas]$ awk -v i=1 -F: '{print $3+i}' passwd
1
2
3
4
- awk的內置變量
表1-56
變量 |
說明 |
FILENAME |
文件名 |
NR |
已讀的記錄數 |
NF |
瀏覽記錄的域的個數(切割后,列的個數) |
- 案例實操
(1)統計passwd文件名,每行的行號,每行的列數
[atguigu@hadoop102 datas]$ awk -F: '{print "filename:" FILENAME ", linenumber:" NR ",columns:" NF}' passwd
filename:passwd, linenumber:1,columns:7
filename:passwd, linenumber:2,columns:7
filename:passwd, linenumber:3,columns:7
(2)切割IP
[atguigu@hadoop102 datas]$ ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk -F " " '{print $1}'
192.168.1.102
(3)查詢sed.txt中空行所在的行號
[atguigu@hadoop102 datas]$ awk '/^$/{print NR}' sed.txt
5
四、 sort
sort命令是在Linux里非常有用,它將文件進行排序,並將排序結果標准輸出。
- 基本語法
sort(選項)(參數)
表1-57
選項 |
說明 |
-n |
依照數值的大小排序 |
-r |
以相反的順序來排序 |
-t |
設置排序時所用的分隔字符 |
-k |
指定需要排序的列 |
參數:指定待排序的文件列表
2. 案例實操
(0)數據准備
[atguigu@hadoop102 datas]$ touch sort.sh
[atguigu@hadoop102 datas]$ vim sort.sh
bb:40:5.4
bd:20:4.2
xz:50:2.3
cls:10:3.5
ss:30:1.6
(1)按照“:”分割后的第三列倒序排序。
[atguigu@hadoop102 datas]$ sort -t : -nrk 3 sort.sh
bb:40:5.4
bd:20:4.2
cls:10:3.5
xz:50:2.3