使用:sed [-nefr] action
action:-i直接修改读取的档案内容,而不是由屏幕输出,-r表示支持延伸型正则表达式的语法。
动作说明:[n1[,n2]] function n1,n2表示要选择的行数,function包括:
a-新增,c-取代,d-删除,i-插入,p-打印,s-取代(可以直接进行取代的工作,例如 1,20s/old/new/g)

1 替换第一个匹配的内容

[root@limt01 ~]# echo " This is a test1 test2"|sed 's/test/TEST/'
This is a TEST1 test2

2 替换所有匹配的内容

[root@limt01 ~]# echo " This is a test1 test2"|sed 's/test/TEST/g'
This is a TEST1 TEST2

3 -i 直接修改源文件

sed -i “s/my/Rango Chen's/g” pets.txt

4 在每一行最前面加上#

[root@limt01 ~]# sed 's/^/#/g' pets.txt
#This is my cat
#my cat's name is betty
#This is my dog
#my dog's name is frank
#This is my fish
#my fish's name is george
#This is my goat
#my goat's name is adam

5 在每一行的末尾加上---

[root@limt01 ~]# sed 's/$/ --- /g' pets.txt
This is my cat ---
my cat's name is betty ---
This is my dog ---
my dog's name is frank ---
This is my fish ---
my fish's name is george ---
This is my goat ---

6 只替换第3到第6行的文本

[root@limt01 ~]# sed "3,6s/my/your/g" pets.txt
This is my cat
my cat's name is betty
This is your dog
your dog's name is frank
This is your fish
your fish's name is george
This is my goat
my goat's name is adam

7 只替换第3行的文本

[root@limt01 ~]# sed "3s/my/your/g" pets.txt
This is my cat
my cat's name is betty
This is your dog
my dog's name is frank
This is my fish
my fish's name is george
This is my goat
my goat's name is adam

8 只替换每一行的第二个s

sed 's/s/S/2' my.txt    2表示第二个

9 一次替换多个模式,每个模式之间用;进行间隔

sed '1,3s/my/your/g; 3,$s/This/That/g' my.txt
sed -e'1,3s/my/your/g' -e '3,$s/This/That/g' my.txt

基础的正则表示法特殊字符:

^ 表示一行的开头。如:/^#/ 以#开头的匹配。
$ 表示一行的结尾。如:/}$/ 以}结尾的匹配。
\< 表示词首。如\<abc 表示以abc 为首的詞。
\> 表示词尾。如 abc\> 表示以abc 結尾的詞。
\ 将特殊字符进行转义,还原其本身意义:grep -n \' pets.txt 搜寻含有单引号'的那一行。
. 表示任何单个字符。
* 表示某个字符出现了0次或多次。
[ ] 字符集合。如:[abc]表示匹配a或b或c,还有[a-zA-Z]表示匹配所有的26个字符。如果其中有^表示取反,如[^a]表示非a的字符
\{n,m\} 连续n到m个的“前一个RE字符” grep -n 'go\{2,3\}g' 1.txt 在g与g之间有2到3个o存在的字符串,亦即(goog)(gooog)

文件处理命令:sed的更多相关文章

  1. Linux文件编辑之sed命令

    文件编辑之sed命令 sed是一种流编辑器,它是文本处理中非常重要的工具,能够完美配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时缓冲区中,称为模式空间 (pattern space ...

  2. linux下的文本处理命令sed&awk&grep

    Sedsed 是个精简的.非交互式的编辑器.他能执行和编辑vi和emacs相同的编辑任务.sed编辑器不提供交互使用方式:只能在命令行输入编辑命令.指定文件名,然后在屏幕上察看输出.sed编辑器没有破 ...

  3. [转]linux查看日志文件内容命令

    linux查看日志文件内容命令tail.cat.tac.head.echo tail -f test.log你会看到屏幕不断有内容被打印出来. 这时候中断第一个进程Ctrl-C, ---------- ...

  4. linux查看日志文件内容命令tail、cat、tac、head、echo

    linux查看日志文件内容命令tail.cat.tac.head.echo tail -f test.log你会看到屏幕不断有内容被打印出来. 这时候中断第一个进程Ctrl-C, ---------- ...

  5. 【Linux】linux查看日志文件内容命令tail、cat、tac、head、echo

    linux查看日志文件内容命令tail.cat.tac.head.echo tail -f test.log你会看到屏幕不断有内容被打印出来. 这时候中断第一个进程Ctrl-C, ---------- ...

  6. 笔记02 linux的一些命令sed

    #!/bin/bash # dataformat=`date +%Y-%m-%d-%H-%M` #进行文件件cp并重命名 nginx_home=/opt/modules/nginx-1.12/ cp ...

  7. linux查看日志文件内容命令(面试被常问到的问题)

    tail -f test.log 你会看到屏幕不断有内容被打印出来. 这时候中断第一个进程Ctrl-C, linux 如何显示一个文件的某几行(中间几行) 从第3000行开始,显示1000行.即显示3 ...

  8. linux查看日志文件内容命令tail、cat、tac、head、echo、vi

    linux查看日志文件内容命令tail.cat.tac.head.echo tail -f test.log你会看到屏幕不断有内容被打印出来. 这时候中断第一个进程Ctrl-C, ---------- ...

  9. linux查看日志文件内容命令tail、cat、tac、head、echo详解

    linux查看日志文件内容命令tail.cat.tac.head.echo tail -f test.log你会看到屏幕不断有内容被打印出来. 这时候中断第一个进程Ctrl-C, ---------- ...

  10. Linux查找并删除重复文件的命令行fdupes工具,dupeGuru图形工具

    查了几十个网页,找到这个接近满意的解决方案http://unix.stackexchange.com/questions/146197/fdupes-delete-files-aft... 不过正则里 ...

随机推荐

  1. 数据结构-浙大 MOOC 笔记一 基本概念

    做一些笔记记录自己的学习过程 第一节课介绍了数据结构的基本概念,首先没有直接给出相关的定义而是通过思考如何在书架上摆放书籍这样一个简单的类比了解到数据的组织方式的重要性,并通过printN函数的循环实 ...

  2. 使用angular.bootstrap() 完成模块的手动加载

    之前我们看到使用ng-app指令,可以实现模块的自动加载.现在我们看下,angular中如何手动加载模块.需要使用到angular.bootstrap这个函数. <html> <he ...

  3. web页面跳转的几种方式

    可用客户端触发或服务端触发的方式来实现页面跳转. 客户端触发 方式一:使用Javascript 利用window.location对象的href属性.assign()方法或replace()方法来实现 ...

  4. 基于libvlc和wxWidgets的简单播放器代码阅读

    源代码来自 http://git.videolan.org/?p=vlc.git;a=blob_plain;f=doc/libvlc/wx_player.cpp // g++ wx_player.cp ...

  5. java的各种使用小知识点总结。

    9,重写Arrays.sort public int getHeight(int[][] actors, int n) { // write code here if (null == actors ...

  6. Eclipse设置黑色主题

    1点击help--->install new software 2输入 http://eclipse-color-theme.github.com/update 3下载安装eclipse col ...

  7. DBCP连接池配置示例

    <bean id="dataSourceOracle2" class="org.apache.commons.dbcp.BasicDataSource" ...

  8. Data Validate 之 Data Annotation

    什么是Data Annotation ? 如何使用 ? 自定义Validate Attribute EF  Db first中使用Data Annotation asp.net MVC中使用Data ...

  9. CSS标签

    CSS类选择器被大量使用了 class = cssname

  10. JqueryUI Dialog 加载动态页 部分页

    问题:使用JqueryUIDialog 加载部分页,可以弹出对话框,但是在操作页面上的按钮是提示"dialog"找不到,思路是,先取到部分页加载到要dialog的Div上,在dia ...