日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux下文本处理命令的使用

發(fā)布時(shí)間:2025/6/15 linux 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux下文本处理命令的使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、查看文件的部分截取

1head:顯示文件的開頭幾行,默認(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

---------------------------------------------------------------------------------

2tail:顯示文件的最后幾行,默認(rèn)顯示后10行;

???????? tail? [選項(xiàng)]? 文件名

???????? -n:確定顯示的行數(shù); tail? [n? 行數(shù)] 文件名

???????? -f:可以一直不斷的查看某個(gè)文件的更新; tail? -f? 文件名? 通常用來查看系統(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

…………后面會根據(jù)系統(tǒng)的情況持續(xù)更新顯示,直到按Ctrl - c

---------------------------------------------------------------------------------

?

思考:查看/etc/passwd5-10行的如何打命令呢?

?

三、抽取文本命令:

1、正則表達(dá)式:

???????? [0-9]? [a-z]? [A-Z] 表示一個(gè)集合;

???????? [abc]:匹配列表里的任何一個(gè)字符

???????? [^abc]:匹配列表以外的字符

???????? ^abc:匹配以abc開頭

???????? abc$:匹配以abc結(jié)尾的

?

2grep:顯示文件或標(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

---------------------------------------------------------------------------------

???????? 4abc$

---------------------------------------------------------------------------------

[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í)忽略大小寫;

-n :顯示匹配的行的行號;

-v :過濾掉匹配關(guān)鍵字的行,顯示不匹配的;

---------------------------------------------------------------------------------

[root@localhost ~]# ls

anaconda-ks.cfg? Desktop? install.log? install.log.syslog

[root@localhost ~]# ls |grep -v ^i

anaconda-ks.cfg

Desktop

---------------------------------------------------------------------------------

3cut:顯示文件或者標(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

……下面省略

---------------------------------------------------------------------------------

三、文本分析處理工具:

1wc文本統(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ù),包含不顯示的;

2diff:比較文件:

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ǔ)丁文件名?? 比較文件,然后把不同寫到補(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

---------------------------------------------------------------------------------

3patch:應(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?

---------------------------------------------------------------------------------

4sort:整理文本命令:

????????? 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 符號:使用符號作為字段的定界符;

????????? -k 列數(shù):按照使用的定界符分割的字段的第 列數(shù) 來整理;

---------------------------------------------------------------------------------

[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

?

……后面省略

---------------------------------------------------------------------------------

5tr:把某個(gè)集合內(nèi)的字符換成另外一個(gè)集合中的相應(yīng)的字符

?tr [a-z] [A-Z] <目標(biāo)文件 >新文件名

目標(biāo)文件里的小寫字母替換成大寫然后不存成新文件

---------------------------------------------------------------------------------

[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下文本处理命令的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。