日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Linux命令之乐--sed

發(fā)布時間:2025/3/15 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux命令之乐--sed 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

??  sed是stream edit的縮寫,是處理文本非常重要的工具。

?

常見用法:

1. 替換文本

1.1 替換文本中的第一處符合的樣式

sed 's/pattern/replace_string' file或者cat file | sed 's/pattern/replace_string'

1.2 替換全局的符合的樣式

sed 's/pattern/replace_string/g' file

1.3 直接修改原文件

sed -i 's/pattern/replace_string/g' file

1.4 忽略前面N-1的匹配,從N開始替換

$ echo this this this|sed 's/this/THIS/2g' this THIS THIS

1.5 ?同時替換多個樣式,中間用;隔開

$ echo a b c|sed 's/a/d/g;s/b/e/g;s/c/f/g' d e f

 

2. ?刪除匹配項

sed '/pattern/d' file

 常用于刪除空白行

sed '/^$/d' file

  

3. 已匹配的字符串標記&

echo hello China|sed 's/China/[&]/g' hello [China]

?

4. 子串標記,匹配給定樣式中的一部分

echo this is dight 7 in a number | sed 's/dight \([0-9]\)/\1/' this is 7 in a number

?

工作中的應(yīng)用:

給文本中的每一行添加""

$ sed 's/^/\"&/g;s/$/&\"/g;s/\r//g' file "hello" "linux"

  

  

轉(zhuǎn)載于:https://www.cnblogs.com/zydev/p/7027494.html

總結(jié)

以上是生活随笔為你收集整理的Linux命令之乐--sed的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。