Linux命令之乐--sed
生活随笔
收集整理的這篇文章主要介紹了
Linux命令之乐--sed
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?? 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' file1.3 直接修改原文件
sed -i 's/pattern/replace_string/g' file1.4 忽略前面N-1的匹配,從N開始替換
$ echo this this this|sed 's/this/THIS/2g' this THIS THIS1.5 ?同時(shí)替換多個(gè)樣式,中間用;隔開
$ echo a b c|sed 's/a/d/g;s/b/e/g;s/c/f/g' d e f
2. ?刪除匹配項(xiàng)
sed '/pattern/d' file常用于刪除空白行
sed '/^$/d' file
3. 已匹配的字符串標(biāo)記&
echo hello China|sed 's/China/[&]/g' hello [China]?
4. 子串標(biāo)記,匹配給定樣式中的一部分
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#的基础数据类型
- 下一篇: SAS笔记(2) RETAIN语句