文本提取工具(查看文本) 文本分析工具 文本操作工具 1、文本提取工具 cat、more、less查看文本內容 cat:列印一個或多個文件到標準輸出 1 #合併文件 2 [root@example tmp]# cat file1.txt file2.txt > file3.txt 3 4 #查看文件 ...
文本提取工具(查看文本)
文本分析工具
文本操作工具
1、文本提取工具
-
cat、more、less查看文本內容
-
cat:列印一個或多個文件到標準輸出
1 #合併文件 2 [root@example tmp]# cat file1.txt file2.txt > file3.txt 3 4 #查看文件行號 5 [root@example tmp]# cat -n /tmp/passwd | grep 12 6 7 #查看文件中是否存在特殊字元 8 [root@example tmp]# cat -A test.txt 9 #!/bin/bash$ 10 cat >> err.txt <<EOF$ 11 hostname:example.com$ 12 cpu:2h$ 13 mem:4096M$ 14 ip:192.168.200.154.111$ 15 EOF$
-
more:瀏覽文件內容,每次只看一頁
-
less:瀏覽文件內容,每次只看一頁
-
-/text:搜索text
-
-n/N:跳轉到next/previous匹配的地方
-
-v:用文本編輯器打開該文件
-
man命令中是採用less來分頁的
-
-
head、tail過濾文本內容
-
head:顯示文件的起始10行
-
使用 -n選項指定顯示的行
-
-
tail:顯示文件最後10行
-
使用-n選項指定顯示的行
-
使用-f選項將文件末尾追加的內容顯示在當前終端
-
對於監控日誌文件非常有用
-
如果需要在指定文本中取出內容(以行取)
-
先用head取出最大行
-
然後用 tail 減掉所需行數
-
-
grep 文本過濾工具
-
-o:顯示所有關鍵字
-
-i:忽略大小寫
-
-n:顯示行號
-
-c:顯示行數
-
-v:顯示不匹配的行(取反)
-
-q:靜默模式,沒有任何輸出,得用
-
-AX:將匹配行及其後X行一起顯示
-
-BX:將匹配行及其前X行一起顯示
-
-CX:將匹配行及其前後X行一起顯示
-
-r:遞歸搜索目錄,根據文本內容搜索文件
-
-l:如果匹配成功,則只將文件名列印出來,失敗則不列印,通常 -rl 一起用;例如:grep -rl ‘root’ /etc
-
-
1 #過濾關鍵字 2 [root@example tmp]# grep root passwd 3 root:x:0:0:root:/root:/bin/bash 4 operator:x:11:0:operator:/root:/sbin/nologin 5 6 #顯示所有關鍵字 7 [root@example tmp]# grep -o root passwd 8 root 9 root 10 root 11 root 12 13 #忽略大小寫 14 [root@example tmp]# grep -i root passwd 15 root:x:0:0:root:/root:/bin/bash 16 operator:x:11:0:operator:/root:/sbin/nologin 17 18 #顯示行號 19 [root@example tmp]# grep -n root passwd 20 1:root:x:0:0:root:/root:/bin/bash 21 10:operator:x:11:0:operator:/root:/sbin/nologin 22 23 #取反 24 [root@example tmp]# grep -v root passwd 25 bin:x:1:1:bin:/bin:/sbin/nologin 26 27 #0表示上一條命令執行成功 28 [root@example tmp]# grep -q root passwd 29 [root@example tmp]# echo $? 30 0 31 #1表示上一條命令執行失敗 32 [root@example tmp]# echo $? 33 1 34 35 [root@example var]# grep -A4 ftp passwd 36 [root@example var]# grep -B4 ftp passwd 37 [root@example var]# grep -C4 ftp passwd 38 39 [root@example var]# grep -rl passwd /etc 40 /etc/nsswitch.conf.bak 41 /etc/login.defs 42 /etc/security/pwquality.conf 43 44 [root@example var]# alias 45 alias cp='cp -i' 46 alias egrep='egrep --color=auto' 47 alias fgrep='fgrep --color=auto' 48 #取消高亮 49 [root@example var]# unalias grep 50 #打開高亮 51 [root@example var]# alias grep='grep --color=auto' 52 [root@example var]# source /etc/profile
4. 正則表達式
又稱標準正則表達式,是最早的正則表達式規範,僅支持最基本的元子符集。基本正則表達式是POSIX規範制定的兩種正則表達式語法標準之一,另外一種語法標準稱為擴展正則表達式。
1 [root@example var]# grep bash$ /etc/passwd 2 root:x:0:0:root:/root:/bin/bash 3 user:x:1000:1000:user:/home/user:/bin/bash 4 zhangsan:x:1005:1006::/home/zhangsan:/bin/bash
含義 | |
---|---|
^ | 在每行的開始進行匹配 |
$ | 在每行的末尾進行匹配 |
\ < | 在字的開始進行匹配 |
\ > | 在字的末尾進行匹配 |
. | 對任何單個字元進行匹配 |
[str] | 對str中的任何單個字元進行匹配 |
[^str] | 對任何不在str中的 單個字元進行匹配 |
[a-b] | 對a到b之間的任何字元進行匹配 |
\ | 轉義字元,抑制後面的一個字元的特殊含義 |
* |
grep使用擴展的正則需要使用egrep或者是grep -E
1 [root@example etc]# egrep roo passwd 2 root:x:0:0:root:/root:/bin/bash 3 operator:x:11:0:operator:/root:/sbin/nologin 4 [root@example etc]# grep -E roo+ passwd 5 root:x:0:0:root:/root:/bin/bash 6 operator:x:11:0:operator:/root:/sbin/nologin 7 [root@example etc]# grep -E 'bash|user' passwd 8 root:x:0:0:root:/root:/bin/bash 9 qemu:x:107:107:qemu user:/:/sbin/nologin 10 [root@example etc]# grep -E '(roo?)' passwd 11 root:x:0:0:root:/root:/bin/bash 12 operator:x:11:0:operator:/root:/sbin/nologin 13 14 [root@example etc]# ifconfig | grep -Eo '([0-9]{,3}\.){3}1{1}..' 15 192.168.200.154 16 127.0.0.1 17 192.168.122.1 18 19 [root@example etc]# ifconfig | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'|grep -v 255 20 192.168.200.154 21 127.0.0.1 22 192.168.122.1 23 24 [root@example etc]# ifconfig | grep -w inet | cut -d " " -f10 25 192.168.200.154 26 127.0.0.1 27 192.168.122.1
-
cut - 提取列或欄位
-
顯示文件指定的列或者標準輸入數據
-
cut -d: -f1 /etc/passwd; cut -d : -f1,3 passwd
-
grep root /etc/passwd | cut -d:-f7
-
-
-d:來指定列分隔符
-
-f: 來指定要列印的列
-
-c:指定按字元提取
-
cut -c 2-5 /usr/share/dict/words
-
-
awk 以空作為預設的分隔符
1 [root@example ~]# ifconfig | grep -w inet | awk -F " " '{print $2}' 2 [root@example ~]# awk -F : '{print $1,$3}' /etc/passwd 3 [root@example ~]# ifconfig | grep -w inet | cut -d " " -f10 4 [root@example ~]# ifconfig | grep -w inet | cut -d " " -f10 | cut -d . -f4 | cut -c 1-2 5 [root@example ~]# cut -c 1-4 /etc/passwd
2、文本分析工具
1 [root@example tmp]# wc passwd 2 48 107 2636 passwd 3 第一列是文件的行數 4 第二列是文件的單詞數 5 第三列是文件的位元組數 6 [root@example tmp]# wc -l passwd 7 48 passwd 8 [root@example tmp]# wc -w passwd 9 107 passwd 10 [root@example tmp]# wc -c passwd 11 2636 passwd 12 [root@example tmp]# wc -wc passwd 13 107 2636 passwd
-
文本排序:sort
sort預設按照字元表的順序排序,不是按照單詞或者數字的方式排序
-
-n:以數字的方式進行排正序
-
-r:排倒序
-
-k:指定列
-
-t:指定分隔符
-
-u:去重
-
1 以數字進行排序 -n 2 [root@example tmp]# sort -t : -k 3 -n passwd 3 以數字進行倒序 -r 4 [root@example tmp]# sort -t : -k 3 -r -n passwd
-
文本比較:diff
1 [root@example tmp]# diff /etc/passwd /tmp/passwd 2 47a48 3 > user2:x:1002:1002::/home/user2:/bin/bash 4 [root@example tmp]# vimdiff /etc/passwd /tmp/passwd 5 2 files to edit
3、文本操作工具
-
文本轉換工具:tr
1 [root@example tmp]# tr a-z 1-2 < passwd 2 2222:2:0:0:2222:/2222:/222/2122
-
流編輯器:sed
用來進行文本的操作;查找、替換、刪除、新增
-
地址定界:指的是要操作的行
-
#:為數字,指定要進行處理操作的行
-
$_:表示最後一行,多個文件進行操作的時候,為最後一個文件的最後一行
-
/regexp/:表示能夠被regexp匹配到的行,regexp及基於正則表達式的匹配
-
/regexp/l:匹配時忽略大小寫
-
\%regexp%:任何能夠被regexp匹配到的行,換用%(用其它字元也可以,如:#)為邊界符號,當內容出現 \ 時使用
-
addr1,addr2:指定範圍內的所有的行(範圍選定)常用地址界表示方式
-
0,/regexp/:從起始行開始到第一次能夠被regexp匹配到的行
-
/regexp/,/regexp/:被模式匹配到的行內地的所有的行
-
-
first~step:指定起始的位置及步長,例如:1 ~ 2表示1,3,5…
-
addr1,+N:指定行以及以後的N行
-
-
1 [root@example tmp]# sed -n 1p passwd 2 root:x:0:0:root:/root:/bin/bash 3 [root@example tmp]# sed -n 10,12p passwd 4 operator:x:11:0:operator:/root:/sbin/nologin 5 games:x:12:100:games:/usr/games:/sbin/nologin 6 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 7 [root@example ~]# sed -n '/^root/p' /etc/passwd 8 root:x:0:0:root:/root:/bin/bash 9 [root@example ~]# sed -n '$p' /etc/passwd 10 zhangsan:x:1001:1001::/home/zhangsan:/bin/bash
-
p:列印模式空間的內容
-
d:刪除匹配到的內容
-
-i:將操作保存到文件
-
a\text:append,表示在匹配到的行之後追加內容
-
i\text:insert,表示在匹配到的行之前追加內容
-
c\text:change,表示把匹配到的行和給定的文本進行交換
-
s/regexp/replacement/flages:查找替換,把text替換為 regexp 匹配到的內容(其中/可以用其它字元代替,例如@)
-
其它編輯命令
-
g:全局替換,預設只替換第一個
-
i:不區分大小寫
-
p:如果成功替換則列印
-
-
1 [root@example ~]# sed '1d' /etc/passwd 2 [root@example ~]# sed -i '1d' /etc/passwd 3 [root@example ~]# sed '/ftp/i\text' /tmp/passwd 4 [root@example ~]# sed '/ftp/a\text' /tmp/passwd 5 [root@example ~]# sed '/ftp/c\text' /tmp/passwd 6 [root@example ~]# sed -n 's/ftp/http/p' /tmp/passwd 7 http:x:14:50:FTP User:/var/ftp:/sbin/nologin 8 [root@example ~]# sed '46,48 s/ftp/http/gip' /tmp/passwd 9 [root@example ~]# sed '/root/ w /tmp/zhangsan.txt' /tmp/passwd
註意事項:
-
如果沒有指定地址,表示命令將應用於每一行
-
如果只有一個地址,表示命令將應用於這個地址匹配的所有行
-
如果指定了由逗號分隔的兩個地址,表示命令應用於匹配第一個地址和第二地址之間的行(包括這兩行)
-