Linux下文本处理命令的使用
一、查看文件的部分截取
1、head:顯示文件的開(kāi)頭幾行,默認(rèn)顯示前10行;
???????? head? [–n? 行數(shù)] 文件名
---------------------------------------------------------------------------------
[root@localhost ~]# head -n 3 /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
---------------------------------------------------------------------------------
2、tail:顯示文件的最后幾行,默認(rèn)顯示后10行;
???????? tail? [選項(xiàng)]? 文件名
???????? -n:確定顯示的行數(shù); tail? [–n? 行數(shù)] 文件名
???????? -f:可以一直不斷的查看某個(gè)文件的更新; tail? -f? 文件名? 通常用來(lái)查看系統(tǒng)日志;調(diào)試服務(wù)、make程序時(shí)使用;直到按Ctrl-c為止。
---------------------------------------------------------------------------------
[root@localhost ~]# tail -f /var/log/messages
Aug 23 11:27:55 localhost syslogd 1.4.1: restart.
Aug 23 12:46:49 localhost NET[7159]: /sbin/dhclient-script : updated /etc/resolv.conf
…………后面會(huì)根據(jù)系統(tǒng)的情況持續(xù)更新顯示,直到按Ctrl - c
---------------------------------------------------------------------------------
?
思考:查看/etc/passwd第5行-第10行的如何打命令呢?
?
三、抽取文本命令:
1、正則表達(dá)式:
???????? [0-9]? [a-z]? [A-Z]等 表示一個(gè)集合;
???????? [abc]:匹配列表里的任何一個(gè)字符
???????? [^abc]:匹配列表以外的字符
???????? ^abc:匹配以abc開(kāi)頭
???????? abc$:匹配以abc結(jié)尾的
?
2、grep:顯示文件或標(biāo)準(zhǔn)輸入中匹配的文本內(nèi)容
?? 下面我們看一下grep和正規(guī)表達(dá)式一起使用的案例:
???????? 1)[abc]:
---------------------------------------------------------------------------------
[root@localhost ~]# ls
anaconda-ks.cfg? Desktop? install.log? install.log.syslog
[root@localhost ~]# ls |grep '[ai]n'
anaconda-ks.cfg
install.log
install.log.syslog
---------------------------------------------------------------------------------
???????? 2) [^abc]
---------------------------------------------------------------------------------
[root@localhost ~]# ls
anaconda-ks.cfg? Desktop? install.log? install.log.syslog? test
[root@localhost ~]# ls |grep '[^i]n'
anaconda-ks.cfg
---------------------------------------------------------------------------------
???????? 3)^abc
---------------------------------------------------------------------------------
[root@localhost ~]# ls
anaconda-ks.cfg? Desktop? install.log? install.log.syslog? test
[root@localhost ~]# ls |grep '^in'
install.log
install.log.syslog
---------------------------------------------------------------------------------
???????? 4)abc$
---------------------------------------------------------------------------------
[root@localhost ~]# ls
anaconda-ks.cfg? Desktop? install.log? install.log.syslog? test
[root@localhost ~]# ls|grep 'log$'
install.log
install.log.syslog
---------------------------------------------------------------------------------
?? grep命令選項(xiàng):
-i :搜索匹配的關(guān)鍵詞時(shí)忽略大小寫(xiě);
-n :顯示匹配的行的行號(hào);
-v :過(guò)濾掉匹配關(guān)鍵字的行,顯示不匹配的;
---------------------------------------------------------------------------------
[root@localhost ~]# ls
anaconda-ks.cfg? Desktop? install.log? install.log.syslog
[root@localhost ~]# ls |grep -v ^i
anaconda-ks.cfg
Desktop
---------------------------------------------------------------------------------
3、cut:顯示文件或者標(biāo)準(zhǔn)輸入數(shù)據(jù)的指定的列
???????? cut –d區(qū)分分割的定界符 –f 要顯示的列的編碼 文件名
???????? -d:指定區(qū)分的定界符,默認(rèn)為TAB
???????? -f:指定要顯示的列的編碼
---------------------------------------------------------------------------------
[root@server ~]# cut -d: -f1 /etc/passwd
root
Bin
daemon
……下面省略
---------------------------------------------------------------------------------
三、文本分析處理工具:
1、wc文本統(tǒng)計(jì):
???????? wc? [選項(xiàng)]? 目標(biāo)文件
---------------------------------------------------------------------------------
[root@server ~]# wc? /etc/passwd
? 35???????? ?54????????????? 1589?????? /etc/passwd
行數(shù)??? 單次總數(shù)?? 字節(jié)總數(shù)
---------------------------------------------------------------------------------
???????? -l:只統(tǒng)計(jì)行數(shù)
???????? -w:只統(tǒng)計(jì)單次總數(shù)
???????? -c:只統(tǒng)計(jì)字節(jié)數(shù)
???????? -m:只統(tǒng)計(jì)字符總數(shù),包含不顯示的;
2、diff:比較文件:
diff? 文件1? 文件2?
---------------------------------------------------------------------------------
[root@server ~]# diff install.log install.log1
9c9
< 安裝 nash-5.1.19.6-54.i386
---
> 裝 nash-5.1.19.6-54.i386
---------------------------------------------------------------------------------
diff –u 文件1? 文件2 >補(bǔ)丁文件名?? 比較文件,然后把不同寫(xiě)到補(bǔ)丁文件中
---------------------------------------------------------------------------------
[root@localhost ~]# cat test test1
this is a test
where are yourhoume?
this is a exam
where are yourtown?
[root@localhost ~]# diff -u test test1 >test.patch
[root@localhost ~]# cat test.patch
--- test??????? 2010-08-26 15:17:31.000000000 +0800
+++ test1?????? 2010-08-26 15:17:56.000000000 +0800
@@ -1,2 +1,2 @@
-this is a test
-where are yourhoume?
+this is a exam
+where are yourtown?
[root@localhost ~]# ls
anaconda-ks.cfg? Desktop? install.log? test? test1? test.patch
---------------------------------------------------------------------------------
3、patch:應(yīng)用文件在其他文件中的改變
??? patch [-b] 目標(biāo)文件名 .patch的比較文件
??? .patch的文件:由diff命令比較創(chuàng)建
-b:備份目標(biāo)文件;
---------------------------------------------------------------------------------
[root@localhost ~]# cat test test1
this is a test
where are yourhoume?
this is a exam
where are yourtown?
[root@localhost ~]# patch -b ./test test.patch
patching file ./test
[root@localhost ~]# cat test
this is a exam
where are yourtown?
---------------------------------------------------------------------------------
4、sort:整理文本命令:
????????? sort? [選項(xiàng)]? 文件
????????? -r :執(zhí)行反方向整理(有上之下)
---------------------------------------------------------------------------------
[root@server ~]# grep bash? /etc/passwd|sort
op:x:501:501::/home/op:/bin/bash
redhat:x:500:500::/home/redhat:/bin/bash
root:x:0:0:root:/root:/bin/bash
[root@server ~]# grep bash? /etc/passwd|sort -r
root:x:0:0:root:/root:/bin/bash
redhat:x:500:500::/home/redhat:/bin/bash
op:x:501:501::/home/op:/bin/bash
---------------------------------------------------------------------------------
????????? -n:按照數(shù)字大小整理
????????? -u:刪除輸出中的重復(fù)行;
????????? -t 符號(hào):使用符號(hào)作為字段的定界符;
????????? -k 列數(shù):按照使用的定界符分割的字段的第 列數(shù) 來(lái)整理;
---------------------------------------------------------------------------------
[root@server ~]# sort -t : -k 3 -r /etc/passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
news:x:9:13:news:/etc/news:
sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
?
……后面省略
---------------------------------------------------------------------------------
5、tr:把某個(gè)集合內(nèi)的字符換成另外一個(gè)集合中的相應(yīng)的字符
?tr ‘[a-z]’ ‘[A-Z]’ <目標(biāo)文件 >新文件名
目標(biāo)文件里的小寫(xiě)字母替換成大寫(xiě)然后不存成新文件;
---------------------------------------------------------------------------------
[root@localhost ~]# tr '[a-z]' '[A-Z]' <anaconda-ks.cfg >an.bak
[root@localhost ~]# cat an.bak
# THE FOLLOWING IS THE PARTITION INFORMATION YOU REQUESTED
# NOTE THAT ANY PARTITIONS YOU DELETED ARE NOT EXPRESSED
# HERE SO UNLESS YOU CLEAR ALL PARTITIONS FIRST, THIS IS
# NOT GUARANTEED TO WORK
---------------------------------------------------------------------------------
?
?
轉(zhuǎn)載于:https://blog.51cto.com/iminmin/384014
總結(jié)
以上是生活随笔為你收集整理的Linux下文本处理命令的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 震惊:菲律宾总统咧嘴冷笑视察惨剧!
- 下一篇: WinRAR最新版V3.93 破解方法