歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> Linux文本處理命令(wc,cut,sort,uniq,diff,patch)詳解

Linux文本處理命令(wc,cut,sort,uniq,diff,patch)詳解

日期:2017/3/6 9:23:58   编辑:學習Linux

Linux文本處理命令(wc,cut,sort,uniq,diff,patch)詳解


Linux文本處理命令(wc,cut,sort,uniq,diff,patch)詳解


我相信大家在使用Linux過程中總會遇到想要提取某些自己需要的信息的情況,比如如下這四種情況:
1、找出ifconfig命令結果中eno16777728的IPv4地址
2、查出分區空間使用率的最大百分比值
3、查出/tmp的權限,以數字方式顯示
這個時候,我們使用命令當然也可以查看,不過還需要自己通過眼睛去過濾不需要的信息,多費勁。如何讓自己更輕松的看到自己想看到自己想看的信息呢?今天的文本處理命令能滿足我們的簡單需求。

wc 此wc非彼WC,在這裡wc是word count的簡寫

wc - print newline, word, and byte counts for each file
其表達格式:wc [OPTION]... [FILE]...
常用選項:
-l:lines 只顯示行數
-w:words 只顯示單詞總數
-c:bytes 只顯示該內容字節總數

下面以實例來具體顯示wc的功用
創建/test目錄 創建/test/wc.txt文件
[root@localhost test]# cat > 1
hello me
hello my boy

使用cat在wc.txt內輸入一些字符

[root@localhost test]# wc wc.txt
2 5 22 wc.txt 第一個2代表行數 第二個5代表單詞數 第三個22代表該內容字節總數
[root@localhost test]# wc -l wc.txt
2 wc.txt
[root@localhost test]# wc -c wc.txt
22 wc.txt
[root@localhost test]# wc -w wc.txt
5 wc.txt

cut
cut - remove sections from each line of files
表達格式:cut OPTION... [FILE]...
常用選項:
-d<char> : 以指定的字符為分隔符
-f #(單個字段)|#-#(連續多個字段)|#,...,#(離散多個字段)
-c 按字符切割
--output-delimiter=STRING指定輸出分隔符
以/etc/passwd文件為對象做實驗
1、取用戶名及用戶UID並指定輸出的分隔符為#

[root@localhost test]# tail -5 /etc/passwd
laowang:x:4322:4322::/home/laowang:/bin/bash
u1:x:4323:4323:UUU:/home/u1:/bin/csh
u2:x:4324:4324::/home/u2:/bin/bash
u3:x:4325:4325::/home/u3:/bin/bash
u4:x:4326:4326::/home/u4:/sbin/nologin

通過上面內容,我們可以確定我們需要的內容在第一節跟第三節,同事分隔符為“:”
[root@localhost test]# cut -d: -f 1,3 /etc/passwd --output-delimiter=#
root#0
bin#1
daemon#2
adm#3
..

2、查看/etc/passwd文件最後一行,且從第5個字符開始截取到第十個字符.
[root@localhost test]# tail -1 /etc/passwd | cut -c 5-10
:4326:

sort 排序

sort - sort lines of text files
表達格式: sort [OPTION]... [FILE]...
常用選項:
-t CHAR:指定分隔符
-k #:用於排序比較的字段
-n:基於數值大小排序
-r:逆序排列
-f:忽略字符大小寫
-u:重復內容只保留一行
還是以/etc/passwd為對象測試
顯示UID最大的用戶及其默認shell
[root@localhost test]# sort -t: -k 3 -n /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
adm:x:3:4:adm:/var/adm:/sbin/nologin
...
basher:x:4329:4329::/home/basher:/bin/bash
nologin:x:4330:4330::/home/nologin:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

顯示成功,但是結果並不直觀,我們在利用上面的cut命令對結果進行進一步加工

[root@localhost test]# sort -t: -k 3 -n /etc/passwd | tail -1 | cut -d: -f 1,7
nfsnobody:/sbin/nologin

uniq 報告或移除重復的行
uniq - report or omit repeated lines
表達格式:uniq [OPTION]... [INPUT [OUTPUT]]
常用選項:
-c:顯示每行的重復次數
-u:僅顯示未曾重復過的行
-d:僅顯示重復過的行
為了演示uniq命令方便,我們創建一個有重復行的文件/test/uniq.txt
[root@localhost test]# cat uniq.txt
qqqq
qqqq
qqqq
dfsdf
aa
bb
bb
cc
q
[root@localhost test]# uniq uniq.txt
qqqq
dfsdf
aa
bb
cc
q

uniq 加文件默認將重復的內容隱藏。
[root@localhost test]# uniq -c uniq.txt
3 qqqq
1 dfsdf
1 aa
2 bb
1 cc
1 q
1 q
[root@localhost test]# uniq -u uniq.txt
dfsdf
aa
cc
q
[root@localhost test]# uniq -d uniq.txt
qqqq
bb

--------------------------------------------------------------------------------
diff 逐行比較文件的異同

diff - compare files line by line
diff [OPTION]... FILES
常用選項:

-u:使用unified機制,即顯示要修改的行的上下文,默認3行
[root@localhost test]# cat diff1 diff2
abcd
abcde
abcd
bcd
abcde
bc
[root@localhost test]# diff diff1 diff2
1c1
< abcd
---
> bcd
3c3
< abcd
---
> bc
[root@localhost test]# diff -u diff1 diff2
--- diff1 2016-08-05 19:46:36.985538120 +0800
+++ diff2 2016-08-05 19:46:54.951836769 +0800
@@ -1,3 +1,3 @@
-abcd
+bcd
abcde
-abcd
+bc

patch 向文件打補丁
基本概念 patch - apply changes to files
表達格式patch [-blNR][ -c| -e| -n][-d dir][-D define][-i patchfile]
[-o outfile][-p num][-r rejectfile][file]
patch [OPTION] -l /PATH/PATH_FILE /PATH/OLDFILE
patch /PATH/OLDFILE < /PATH/PATH_FILE

--------------------------------------------------------------------------------
常用的簡單文本處理命令介紹完畢,下面來利用本文介紹的命令來解決開頭提出的四個問題

1、找出ifconfig命令結果中eno16777728的IPv4地址
ifconfig | tr -cs '[:digit:].' ':'| cut -d: -f 5
10.1.253.79

以上共分3步
1)先將ifconfig的內容當做tr的基本輸入內容;2)將第一步的內容中所有非數字的內容替換為":"並壓縮;3)看所需的IP在第幾段然後使用cut命令進行切割

2、查出分區空間使用率的最大百分比值
查看分區空間命令為df
[root@localhost test]# df | tr -s ' ' ':'| cut -d: -f 5| tr -d '%'|sort -n|tail -1
29

要實現上述內容需要
1)使用df列出分區空間使用率的內容,
2)之後使用tr將空格替換為:並進行壓縮,
3)再之後使用cut進行切割將使用率的列取出,
4)再使用tr將%剔除,
5)之後使用sort按數值大小進行排序
6)最後再使用tail取最後一行的最大值。

3、查出/tmp的權限,以數字方式顯示
查看/tmp權限可以使用stat,它可以自動顯示處其權限對應的數值,剩下的只需要我們將數字從內容中取出即可。
[root@localhost test]# stat /tmp/ | tr -cs '[:digit:]' ':'| cut -d: -f 9
1777

1)先顯示權限內容
2)將內容中所有非數字替換為“:”並壓縮
3)數出對應的權限數字在第幾段後進行切割

本文永久更新鏈接地址:

http://xxxxxx/Linuxjc/1148615.html TechArticle

Copyright © Linux教程網 All Rights Reserved