Linux下文本处理命令的使用
一、查看文件的部分截取
1、head:顯示文件的開頭幾行,默認顯示前10行;
???????? head? [–n? 行數] 文件名
---------------------------------------------------------------------------------
[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:顯示文件的最后幾行,默認顯示后10行;
???????? tail? [選項]? 文件名
???????? -n:確定顯示的行數; tail? [–n? 行數] 文件名
???????? -f:可以一直不斷的查看某個文件的更新; tail? -f? 文件名? 通常用來查看系統日志;調試服務、make程序時使用;直到按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
…………后面會根據系統的情況持續更新顯示,直到按Ctrl - c
---------------------------------------------------------------------------------
?
思考:查看/etc/passwd第5行-第10行的如何打命令呢?
?
三、抽取文本命令:
1、正則表達式:
???????? [0-9]? [a-z]? [A-Z]等 表示一個集合;
???????? [abc]:匹配列表里的任何一個字符
???????? [^abc]:匹配列表以外的字符
???????? ^abc:匹配以abc開頭
???????? abc$:匹配以abc結尾的
?
2、grep:顯示文件或標準輸入中匹配的文本內容
?? 下面我們看一下grep和正規表達式一起使用的案例:
???????? 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命令選項:
-i :搜索匹配的關鍵詞時忽略大小寫;
-n :顯示匹配的行的行號;
-v :過濾掉匹配關鍵字的行,顯示不匹配的;
---------------------------------------------------------------------------------
[root@localhost ~]# ls
anaconda-ks.cfg? Desktop? install.log? install.log.syslog
[root@localhost ~]# ls |grep -v ^i
anaconda-ks.cfg
Desktop
---------------------------------------------------------------------------------
3、cut:顯示文件或者標準輸入數據的指定的列
???????? cut –d區分分割的定界符 –f 要顯示的列的編碼 文件名
???????? -d:指定區分的定界符,默認為TAB
???????? -f:指定要顯示的列的編碼
---------------------------------------------------------------------------------
[root@server ~]# cut -d: -f1 /etc/passwd
root
Bin
daemon
……下面省略
---------------------------------------------------------------------------------
三、文本分析處理工具:
1、wc文本統計:
???????? wc? [選項]? 目標文件
---------------------------------------------------------------------------------
[root@server ~]# wc? /etc/passwd
? 35???????? ?54????????????? 1589?????? /etc/passwd
行數??? 單次總數?? 字節總數
---------------------------------------------------------------------------------
???????? -l:只統計行數
???????? -w:只統計單次總數
???????? -c:只統計字節數
???????? -m:只統計字符總數,包含不顯示的;
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 >補丁文件名?? 比較文件,然后把不同寫到補丁文件中
---------------------------------------------------------------------------------
[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:應用文件在其他文件中的改變
??? patch [-b] 目標文件名 .patch的比較文件
??? .patch的文件:由diff命令比較創建
-b:備份目標文件;
---------------------------------------------------------------------------------
[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? [選項]? 文件
????????? -r :執行反方向整理(有上之下)
---------------------------------------------------------------------------------
[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:按照數字大小整理
????????? -u:刪除輸出中的重復行;
????????? -t 符號:使用符號作為字段的定界符;
????????? -k 列數:按照使用的定界符分割的字段的第 列數 來整理;
---------------------------------------------------------------------------------
[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:把某個集合內的字符換成另外一個集合中的相應的字符
?tr ‘[a-z]’ ‘[A-Z]’ <目標文件 >新文件名
目標文件里的小寫字母替換成大寫然后不存成新文件;
---------------------------------------------------------------------------------
[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
---------------------------------------------------------------------------------
?
?
轉載于:https://blog.51cto.com/iminmin/384014
總結
以上是生活随笔為你收集整理的Linux下文本处理命令的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 震惊:菲律宾总统咧嘴冷笑视察惨剧!
- 下一篇: linux下开机启动oracle