Linux sed命令使用
sed基本用法:
sed:stream Editor 行編輯器
sed:模式空間
默認不編輯源文件,僅對模式空間中的數據做處理,處理結束后,將模式空間內容打印出來
sed ?[options]'AddressCommand' file ...
是對file中的文件中的符合Address指定地址范圍內的行執行Command編輯命令
? ? ?options:
? ? ? -n:靜默模式
? ? ? 不顯示模式空間中的內容,僅顯示被匹配的內容
? ? ? -i:直接修改原文件
? ? ? -e SCRIPT(腳本) -e SCRIPT:可以同時執行多個腳本 每個-e就可以執行一個 腳本
? ? ? -f /path/to/sed_srcipt:將-e的腳本保存在一個文件中,通過-f讀取此文件執行
? ? ? sed -f /path/to/sed_srcipt
? ? ? -r:表示使用擴展的正則表達式?
Address方式:
1. StartLine,EndLine
格式例如:1,100,從第1行到第100行
$:表示最后一行
2./Pattern(正則表達式)/
例如:/^root/ ?表示以root開頭的行
3. /pattern1/,/pattern2/
表示:第1次被pattern1匹配到的行開始,第1次被pattern2匹配到的行結束,這中間的所有行
4.LineNumber
指定的行
5.StartLine,+N
表示:從指定的StartLine行開始,向后的N行
Command:
d:刪除符合條件的行
? ?$ sed '2d' example-----刪除example文件的第二行。 ?
* ?
$ sed '2,$d' example-----刪除example文件的第二行到末尾所有行。 ?
* ?
$ sed '$d' example-----刪除example文件的最后一行。 ?
* ?
$ sed '/test/'d example-----刪除example文件所有包含test的行。
例如:
[root@localhost data]# sed "1,2d" /etc/issue ? ---> 表示刪除第1和第2行?
Mage Education Learning Services
http://www.magedu.com
[root@localhost data]# cat /etc/issue
CentOS release 6.6 (Final)
Kernel \r on an \m
Mage Education Learning Services
http://www.magedu.com
[root@localhost data]# sed "1d" /etc/issue ? ?--->表示刪除第1行
[root@localhost data]# sed "1,+3d" /etc/issue -->表示刪除第1行以及往后的3行,共4行
[root@localhost data]# sed "/^\//d" /etc/issue --->表示刪除以/開頭的行,模式必須要//夾起來
p:顯示符合條件的行
例如:
[root@localhost data]# cat ./test?
he like her
she is a good boy
what are you doing
[root@localhost data]# sed /he/p ./test ?--->將行中包含he的行顯示出來
he like her
he like her ---->從執行結果看,符合模式匹配的行顯示了2次,原因是sed命令的默認意思
she is a good boy 將模式空間中的命令行處理過后還要顯示出來,顯示的那一行在模式空間中
she is a good boy 還存在,sed命令模式是顯示模式空間,因此能匹配到的顯示2次,如果不想
what are you doing ? ? ? ? ?顯示模式空間中的,可以是用-n,顯示靜默模式,不顯示模式空間的
[root@localhost data]# sed -n /he/p ./test ? --->靜默模式,只顯示符合條件的行
he like her
she is a good boy
? ?c:取代,c 的后面可以接字串,這些字串可以取代 n1,n2 之間的行
? ? 例如:
? ? root@localhost ruby]# cat ab
? ? Hello!
? ? ruby is me,welcome to my blog.
? ? end
? ?[root@localhost data]# sed ?'1,2c hi' ./linux?
hi
end
a \string:在指定的行后面追加新行,內容為string
例如:
[root@localhost data]# sed "/he/a \welcom to linux world" ./test -->在行中有he的行后追加一行
he like her
welcom to linux world
she is a good boy
welcom to linux world
what are you doing?
[root@localhost data]# sed "/he/a \welcom to linux world\nthe two line" ./test --->追加兩行,
在字符間加"\n"表示換行
i \string:在指定的行前面追加新行,內容為string
例如:
[root@localhost data]# sed "/he/i \welcom to linux world" ./test?
welcom to linux world
he like her
welcom to linux world
she is a good boy
what are you doing
r file(文件路徑):將指定的文件的內容,添加至符合條件的行處
例如:
[root@localhost data]# sed '2r ./first.sh' ./test ?--->此處必須要用單引號,雙引號就出錯
he like her
she is a good boy
#!/bin/bash
U=`wc -l ?/etc/rc.d/rc.sysinit |cut -d' ' -f1`
G=`wc -l /etc/rc.d/init.d/functions |cut -d' ' -f1`?
sum=$[$U+$G]
echo $sum
what are you doing?
[root@localhost data]# sed '1,2r ./first.sh' ./test --->在第1行和第2行后都添加
w file(文件路徑):將指定范圍內的內容另存在指定的文件中
例如:
[root@localhost data]# sed '/he/w ./xx.txt' ./test -->將./test文件中符合條件的行保存至./xx.txt
he like her 文件中,事先xx.txt可以不存在,如果多次執行
she is a good boy 保存在xx.txt中是覆蓋追加的,此處還顯示在屏幕
what are you doing 上是因為,工作在模式空間而不是靜默模式
[root@localhost data]# cat ./xx.txt?
he like her
she is a good boy
s/pattern/string/修飾符:將每行中符合pattern模式的行替換為string,默認只替換每行中第一次被模式匹配到的字符串
或s@@@修飾符或s###修飾符都行,string是字符串,不能是正則表達式?
如果要全部替換需要加修飾符
g:全局替換
i:查找時忽略大小寫
&:引用模式匹配整個串
例如:
[root@localhost data]# sed 's/o/T/' ./test --->將查找的每行中第1個"o"的替換為T
he like her
she is a gTod boy
what are yTu doing?
[root@localhost data]# sed 's/o/@/g' ./test -->加上g修飾符,則全部替換
he like her
she is a g@@d b@y
what are y@u d@ing?
[root@localhost data]# sed 's@\(l..e\)@\1r@g' ./test?
he liker her
she is a good boy
what are you doing
[root@localhost data]# sed 's@l..e@&r@g' ./test --->此處@代表之前的整個模式l..e,\1和&在不同的地方有不同的方便
he liker her
she is a good boy
what are you doing
轉載于:https://blog.51cto.com/501steve/1577200
總結
以上是生活随笔為你收集整理的Linux sed命令使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: StaggeredGridView 实现
- 下一篇: JavaScript onerror事件