linux 三大利器 grep sed awk sed
?
?sed主要內(nèi)容和原理介紹
sed 流處理編輯器
?
?
- sed一次處理一行內(nèi)容,讀入一行處理一行
- sed不改變文件內(nèi)容(除非重定向)
?
sed?
- 命令行格式
$ sed [options] 'command' file(s) (option:參數(shù) ,command 操作命令 ,file操作文件命令)
option : -e , -n
command : 行定位(正則)+sed命令(操作)
- 腳本格式
$ sed -f scriptfile file(s)
?
sed 操作命令
- 基本操作命令
p(打印相關(guān)的行)
??1.1 sed 'p' passwd #會(huì)打印出兩行,因?yàn)閟ed的原理是讀入一行,輸出一行,此處再加上p命令打印出來(lái)的一行,所以最后會(huì)打印出兩行
? ? ? ? ? ? ? ? ?1.2 sed -n 'p'passwd #加了-n選項(xiàng)之后,只會(huì)打印出相關(guān)的行,那些不相關(guān)的行則不會(huì)打印出來(lái) 二、行定位
? ?行定位 :
? ? ? ? ? ? ? ? ? ? ? ?定位一行: x; ?/pattern/ ?加入x定位到第x行,也可加入正則定位目標(biāo)行
定位多行 :x,y ; /pattern/,y ?定位到x到y(tǒng)行,或者定位到有正則pattern的行到y(tǒng)行
/pattern1/,/pattern2/; 定位到有正則pattern1的行到有正則pattern2的行
x,y! 定位到除x到y(tǒng)行的其他行
定位間隔行:first~step 定位到first行,在每隔step行輸出一行
?
sed -n '/ff/!p' file
sed -n '/news/,/mooc/p' ?file---從news行開(kāi)始到mooc行截至范圍
sed -n '10,20!p' ?file---10-20行不打印
sed -n '10~3p' file---10行開(kāi)始,跳躍3行輸出
?
a(新增行)/i(插入行)
c(替代行)
? ?d(刪除行)
? ? ? ? ? ? ? ? ? ? ? ?sed '5a======' file#在第5行的下面新增一行,并寫入======這些內(nèi)容? ?
sed '1,5i======' file 表示在1-5行前面插入一行,并寫入======這些內(nèi)容 ??
sed '1,5c======' file 將1-5行改變成一行====
? ??sed '1,5d' file 刪除1-5行
?
轉(zhuǎn)載于:https://www.cnblogs.com/lukechenblogs/p/8630169.html
總結(jié)
以上是生活随笔為你收集整理的linux 三大利器 grep sed awk sed的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: PHP实现简单的双色球机选号码
- 下一篇: Linux 下 zip unzip压缩与