日韩性视频-久久久蜜桃-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ò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲精品白浆 | 中文字幕欧美专区 | 黄色免费网站观看 | 蜜臀视频在线播放 | 国产情侣久久 | 午夜激情在线视频 | 哺乳援交吃奶在线播放 | 欧美激情精品久久久久久变态 | www.国产成人 | 在线播放的av | 一区二区三区四区在线播放 | 亚洲天堂av免费在线观看 | 亚洲人高潮女人毛茸茸 | 久色88 | 欧美激情在线观看视频 | 伊人999 | 亚洲一区二区福利 | 欧美伊人影院 | 嫩模一区| 日韩成人在线免费观看 | 中国美女洗澡免费看网站 | 欧美欧美欧美欧美 | 少妇又色又爽又高潮极品 | 免费看日批视频 | 日本第一页 | 少妇大叫太粗太大爽一区二区 | 自拍偷拍激情视频 | 免费亚洲一区二区 | 中文字母av | 99网站| 免费网站看av | 欧美色图狠狠干 | 竹菊影视一区二区三区 | 婷婷777| 国产精品无码毛片 | 五月六月婷婷 | 合欢视频在线观看 | 欧美日韩中文字幕一区二区 | 色妹av | 一级aaa毛片| 性猛交富婆╳xxx乱大交麻豆 | 久草新在线 | 日韩精品视频免费 | 免费黄色片网站 | 久久亚洲av午夜福利精品一区 | 久久婷婷综合国产 | 99国产精品视频免费观看一公开 | 五月六月婷婷 | 国产女人高潮的av毛片 | 国产黄色小视频在线观看 | 国产美女黄网站 | 天天操中文字幕 | 一本色道久久综合亚洲精品按摩 | www,99| 中文字字幕在线观看 | 黄色三级网络 | 免费成人电影在线观看 | 日本偷拍一区 | yw视频在线观看 | 理伦毛片 | 国产三区在线播放 | 熟睡侵犯の奶水授乳在线 | 欧美一区二区人人喊爽 | 日韩欧美二区三区 | 波多野结衣在线免费视频 | 黄色在线免费播放 | 老头糟蹋新婚少妇系列小说 | 高清不卡毛片 | 欧美片网站yy| 欧美一区二区黄色 | 91色片 | 国产精品v欧美精品v日韩 | 欧美高清二区 | 亚洲天堂资源在线 | 欧美整片在线 | www,超碰| 清纯唯美亚洲激情 | 久久av无码精品人妻系列试探 | 五月婷网站 | 色婷婷av一区二区三区之红樱桃 | 久久久永久久久人妻精品麻豆 | 亚洲天堂免费在线 | 男生和女生差差视频 | 欧美日韩伦理片 | 国产精品久久久免费视频 | 欧美国产一级片 | 中文字幕一区二区三区免费视频 | 国产亚洲三级 | 欧美十大老熟艳星 | 国内9l自拍 | 男女男精品视频网站 | 亚洲αv | 国产精品久久毛片av大全日韩 | 国产人妻777人伦精品hd | 天堂视频免费在线观看 | 美景之屋电影免费高清完整韩剧 | 国产极品在线播放 | 亚洲精品久久久久久久久久吃药 | 小草av在线 |