暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

sed工具

菜涛学Java 2018-12-02
142

sed是一个管道命令,可以分析standard,input;sed也可以将数据进行替换,删除,新增,选取特定行等功能

  1. sed [-nefr] [动作]

  2. -n :经过sed特殊处理的那一行或操作才会被列出来

  3. -e : 直接在命令模式上进行sed动作编辑

以行为单位的新增/删除功能

  1. 在第二行后加上add a row字样

  2. [root@izwz9hpp9gmzo5x5e06iqjz home]# nl regular_express.txt |sed '2a  add a row'

  3.     1    "Open Source" is a good mechanism to develop programs.

  4.     2    apple is my favorite food.

  5. add a row

  6.     3    Football game is not use feet only.



  7. 删除2-5

  8. [root@izwz9hpp9gmzo5x5e06iqjz home]# nl regular_express.txt  |sed 2,5d

  9.     1    "Open Source" is a good mechanism to develop programs.

  10.     6    GNU is free air not free beer.

  11.     7    Her hair is very beauty.

以行为单位的替换与显示功能

  1. 2-5行的内容替换为no 2-5 row

  2. [root@izwz9hpp9gmzo5x5e06iqjz home]# nl regular_express.txt | sed '2,5c no 2-5 row'

  3.     1    "Open Source" is a good mechanism to develop programs.

  4. no 2-5 row

  5.     6    GNU is free air not free beer.

  6. 打印2-5行内容

  7. [root@izwz9hpp9gmzo5x5e06iqjz home]# nl regular_express.txt |sed -n '2,5p'

  8.     2    apple is my favorite food.

  9.     3    Football game is not use feet only.

  10.     4    this dress doesn't fit me.

  11.     5    However, this dress is about $ 3183 dollars.

sed 's/要被替换的字符串/新的字符串/g'

直接修改文件内容(危险操作)

sed -i 's/要被替换的字符串/新的字符串/g'


文章转载自菜涛学Java,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论