Sed引用被替換的表示式

2021-05-23 11:20:11 字數 649 閱讀 8072

sed做表示式替換的時候,可以使用&來表示前面正規表示式的匹配的部分。而且還可以用/1 .../9來引用前面的子表示式的內容。

man sed可以看到如下解釋:

s/regexp/replacement/

attempt to match regexp against the pattern space.  if  success-

ful,   replace  that  portion  matched  with  replacement.   the

replacement may contain the special character & to refer to that

portion  of  the  pattern  space  which matched, and the special

escapes /1 through /9 to refer  to  the  corresponding  matching

sub-expressions in the regexp.

舉個小例子:

[ying@localhost]$ echo "/"tom is a good boy/"" | sed "s#/".*/?#/"/"&/"/"#g"

"""tom is a good boy"""

sed 正規表示式

如果testfile的內容是 welcome to the world of regexp 現在要去掉所有的html標籤,使輸出結果為 hello world welcome to the world of regexp 怎麼做呢?如果用下面的命令 sed s g testfile 結果是兩個空行,...

sed 常用正規表示式

1.乙個比較實用的正規表示式 匹配html的嵌入 匹配 的嵌入碼 刪除僅由空字元組成的行 sed space d filename 匹配html標籤 例如 從html檔案中剔除html標籤 sed s g space d file.html 例如 要從下列 中去除 及其中包括的 b 4c6c2a65...

Linux正規表示式 sed

相比於grep,sed可以替換內容並輸出到螢幕上。sed 選項 動作 filename選項 n 將經過sed命令處理過的行輸出到螢幕 e 允許對輸入資料應用多條sed命令 i 用sed的修改結果直接修改所讀取的檔案 動作 d 刪除 p 列印 s 字串替換 本行 g 本行全域性替換 列印 etc pa...