sed基礎用法小結
Sed包含多個功能,最常用的是替換命令
1 替換
s/pattern/replacement/flags
Flags包括如下
n:1-512之間的數字,對第n次匹配進行替換
g:對所有匹配情況進行全域性修改
p:列印模式所有內容
w:file,將模式內容寫入檔案file
對於replacement,如下字元有特殊含義
&:將其替換為pattern
\n:匹配第n個字串,在pattern中用”\(“和”\)”指定
\:跳脫字元
&替換成pattern,可\&轉義
[oracle@ ~]$ cat test
ORA
[oracle@ ~]$ cat test | sed 's/ORA/O’ Reilly \& Associates, Inc./g'
O’ Reilly & Associates, Inc.
[oracle@ ~]$ cat test | sed 's/ORA/O’ Reilly & Associates, Inc./g'
O’ Reilly ORA Associates, Inc.
[oracle@ ~]$ cat test1
See Section 19.40 See Section 18.89
See Section 19.09
See Section 07.10
[oracle@ ~]$ sed 's/See Section [1-9][0-9]*\.[1-9][0-9]*/(&)/2' test1 –為每行的第二個匹配字元包圍”()”
See Section 19.40 (See Section 18.89)
See Section 19.09
See Section 07.10
將每行第2個See替代為換行字元
[oracle@ ~]$ sed 's/See/\\n/2' test1 --不起作用
See Section 19.40 \n Section 18.89
See Section 19.09
See Section 07.10
[oracle@ ~]$ sed 's/See/\
> /2' test1 --成功換行,但不太好維護
See Section 19.40
Section 18.89
See Section 19.09
See Section 07.10
[oracle@ ~]$ sed 's/See/\n/2' test1 --使用\n替代
See Section 19.40
Section 18.89
See Section 19.09
See Section 07.10
搜尋每行第2個匹配模式,並將其第1/2個元素互換位置
[oracle@ ~]$ sed 's/\(See Section\) \([1-9][0-9]\.[1-9][0-9]\)/\2 \1/2' test1
See Section 19.40 18.89 See Section
See Section 19.09
See Section 07.10
[oracle@ ~]$ sed '/0[1-9]\./ s/\(See Section\) \([0-9][0-9]*\.[1-9][0-9]\)/\2 \1/' test1 –先搜尋包含”0[1-9].”的行,如果其滿足匹配模式,則將其第1/2個元素互換位置
See Section 19.40 See Section 18.89
See Section 19.09
07.10 See Section
[oracle@ ~]$ cat test1 | grep UNIX
UNIX
[oracle@ ~]$ sed 's/UNIX/\\s-1&\\s0/' test1 --使用&替代前面的UNIX
\s-1UNIX\s0
[oracle@ ~]$ sed 's/UNIX/\s-1\&\\s0/' test1 –使用\&將其作為普通字元,改為\s,而s不是特殊字元所以\s等同於s
s-1&\s0
[oracle@ ~]$ sed 's/UNIX/s-1\&\\s0/' test1
s-1&\s0
忽略大小寫
[oracle@ ~]$ cat test5
who finger w last
[oracle@ ~]$ sed 's/FINger/www/gI' test5
who www w last
2 刪除(d)
如果行匹配則刪除該行
/^$/d –刪除空行
[oracle@ ~]$ cat test3
adf
sdf
[oracle@ ~]$ sed '/^$/d' test3 --刪除空行
adf
sdf
[oracle@ ~]$ sed '/./!d' test3 --刪除空行,!表示not,/./!即不帶有字元的行
adf
sdf
[oracle@ ~]$ sed '/./d' test3 – 刪除非空行
[oracle@ ~]$ sed '$d' test3 --刪除最後一行
adf
[oracle@ ~]$ sed '2d' test3 –刪除第2行
adf
sdf
[oracle@ ~]$ sed '$!d' test3 –只保留最後一行資料,其餘全刪除
Sdf
3 追加(a)/插入(i)/更改(c)
格式如下
[line-address][a|i|c] text
注:<<sed and awk>>(O’Reilly)指出 “這些命令必須在多行上指定, (\用於轉義行尾)”
[line-address][a|i|c]\
Text
但個人試驗發現不需要這麼麻煩
[oracle@ ~]$ cat test3
adf
sdf
在第1行和最後1行前後分別新增資訊
[oracle@ ~]$ sed -e '1i insert before the first line' -e '$a append after the last line' test3
insert before the first line
adf
sdf
append after the last line
使用更改(c)命令有時比替換更高效,如下:
將所有以”.sp”開頭的行的輸入引數替換為5
[oracle@ ~]$ cat test4
.sp 1.5
.sp 4
.sp
5.sp 67
[oracle@ ~]$ sed '/^\.sp/c \.sp 5' test4 --搜尋以.sp開頭的行,將整行替代為.sp 5
.sp 5
.sp 5
.sp 5
5.sp 67
4 列表(l)
顯示模式空間的內容,將非列印字元顯示為兩個數字的ASCII程式碼,類似vi的(:l)或者cat -v;
可用於檢測不可見字元;
oracle@ ~]$ cat -v test5
who finger w last^M
^M
^M
[oracle@ ~]$ sed -e "l" test5
who finger w last\r$
who finger w last
\r$
\r$
[oracle@ ~]$ sed -n -e "l" test5
who finger w last\r$
\r$
\r$
其中-n選項作用如下:-n, --quiet, --silent suppress automatic printing of pattern space
5下一行(n)
輸出模式空間的內容,然後讀取輸入的下一行,而不返回指令碼的頂端;
[oracle@usuwsoadb06 ~]$ cat -n test7
1 apple
2 banana
3 mango
4 orange
5 strawberry
[oracle@usuwsoadb06 ~]$ sed '/ban/{n;d;}' test7 --刪除匹配模式的下一行
apple
banana
orange
strawberry
--N與n的區別,前者會輸出當前匹配行
[oracle@usuwsoadb06 ~]$ sed -n '/ban/ {n; p;}' test7
mango
[oracle@usuwsoadb06 ~]$ sed -n '/ban/ {N; p;}' test7
banana
mango
尋找包含ban字元的下一行,將其替換為justin
[oracle@usuwsoadb06 ~]$ sed '/ban/ {n; s/.*/justin/;}' test7
apple
banana
justin --原本的mango現在變為justin
orange
strawberry
6讀寫檔案(r/w)
寫檔案
從某個檔案讀取符合條件的行,並將其寫入另外一個檔案,語法如下:
Sed ‘/pattern/w outputfile’ inputfile
[oracle@ ~]$ cat -n test7
1 apple
2 banana
3 mango
4 orange
5 strawberry
[oracle@ ~]$ sed -n '2,4w test8' test7 –將test7的2-4行輸入到test8
[oracle@ ~]$ cat test8
banana
mango
orange
[oracle@ ~]$ sed -n '/mango/,$w test8' test7 –將test7從包含mango行直到最後一行的資料寫入test8
[oracle@ ~]$ cat test8
mango
orange
strawberry
[oracle@ ~]$ sed -n '$!w test8' test7 –除去最後一行將test7所有內容寫入test8
[oracle@ ~]$ cat test8
apple
banana
mango
orange
讀檔案 --並不會實際改變檔案內容
Sed ‘/pattern/r outputfile’ inputfile
--將檔案內容寫入sed output
[oracle@ ~]$ cat test9
1apple
1banana
1mango
[oracle@ ~]$ cat test10
2orange
2strawberry
[oracle@ ~]$ sed '2,$r test10' test9 –將test10檔案內容插入test9自第2行直至最後一行
1apple
1banana
2orange
2strawberry
1mango
2orange
2strawberry
[oracle@ ~]$ sed '$!r test10' test9—將test10插入test9除最後1行的其餘行之後
1apple
2orange
2strawberry
1banana
2orange
2strawberry
1mango
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25462274/viewspace-2148954/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- sed用法
- Redis基礎——剖析基礎資料結構及其用法Redis資料結構
- Linux sed命令用法Linux
- sed用法點滴
- Linux基礎命令小結Linux
- 【Linux篇】--sed的用法Linux
- Sed常用用法整理
- Linux命令-Sed用法教程Linux
- Linux基礎命令---文字編輯sedLinux
- Java基礎 | Stream流原理與用法總結Java
- webpack用法小結Web
- Promise基礎用法Promise
- ES 基礎用法
- JSTL基礎用法JS
- SqlSugar基礎用法SqlSugar
- Promise 基礎用法Promise
- print基礎用法
- MySQL基礎知識小結(一)MySql
- git 基礎用法梳理Git
- dos-基礎用法
- nodeJS基礎 Stream用法NodeJS
- sed命令小記
- 前面基礎問題小總結(二)
- 『忘了再學』Shell基礎 — 30、sed命令的使用
- 文字三劍客之sed的用法
- css的基礎用法(下)CSS
- Redux 入門 -- 基礎用法Redux
- Linux——基礎命令用法(下)Linux
- Linux——基礎命令用法(上)Linux
- iOS開發基礎篇--NSNotificationCenter使用小結iOS
- java基礎語法知識小結(1)Java
- UI基礎(五)之代理、通知的小結UI
- 060、Vue3+TypeScript基礎,插槽的基礎用法VueTypeScript
- ES6 Promise用法小結Promise
- 【Shell】sed xargs grep awk的組合用法
- JavaScript:async/await的基礎用法JavaScriptAI
- SpringDataMongo連線MongoDB基礎用法SpringMongoDB
- Elaticsearch(一)--基礎原理及用法
- Android技能樹 — 樹基礎知識小結(一)Android