Sed

Sed是一个强大的文本处理工具

能够採用正则匹配。对文本进行插入删除改动等操作

Sed处理的时候,一次处理一行,每一次把当前处理的存放在暂时缓冲区。处理完后输出缓冲区内容到屏幕,然后把下一行读入缓冲区,如此反复,直到结尾。

1、命令格式和參数

sed [-nefr] [动作] 文件

參数:

-n 安静模式。在sed处理的时候。全部来自STDIN的数据都会被输出到终端。加上-n会仅仅输出处理的哪行

-e 直接在命令列上进行sed动作编辑

-f 直接将sed的动作写在文件内

-r sed动作支持延伸的正则表达(默认仅仅是基础正则)

-i 直接改动文件内容(慎用,尤其是用系统文件做练习的时候)





动作:

a append:添加。在当前行的下一行添加

c   :代替,代替n1到n2之间的行

d delete:删除

i 插入,眼下行的上一行插入

p 打印。经常与-n使用

s 代替,s/old/new/g





2、基础使用方法具体解释

(1)第一行之后加入一行

[root@localhost ~]# nl file.txt | sed "1a add text"
1 wtmp begins Mon Feb 24 14:26:08 2014
add text
2 192.168.0.1
3 162.12.0.123
4 this is the last line

(2)第一行之前加入一行

[root@localhost ~]# nl file.txt | sed "1i add text"
add text
1 wtmp begins Mon Feb 24 14:26:08 2014
2 192.168.0.1
3 162.12.0.123
4 this is the last line

(3)删除第2,3行

[root@localhost ~]# nl file.txt | sed "2,3d"
1 wtmp begins Mon Feb 24 14:26:08 2014
4 this is the last line

(4)打印第2,3行

[root@localhost ~]# sed -n "2,3p" file.txt
192.168.0.1
162.12.0.123

这里要提到的是,尽量使用-n。不然会出现这种结果

[root@localhost ~]# sed "2,3p" file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
192.168.0.1
162.12.0.123
162.12.0.123
this is the last line

(5)把168换成169

先看源文件

[root@localhost ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
this is the last line

处理后

[root@localhost ~]# sed "s/168/169/g" file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.169.0.1
162.12.0.123
this is the last line

(6)插入多行

[root@localhost ~]# nl file.txt | sed "2afirst\nsecond" file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
first
second
162.12.0.123
this is the last line

(7)匹配数据,然后进行操作

仅仅须要在上述的基础上加上正则匹配

sed "/匹配的模式/处理的方式" file.txt 

sed "/^root/d" file.txt 对開始有root的删除

比如

匹配begin,并删除改行

[root@localhost ~]# nl file.txt | sed "/begin/d"
2 192.168.0.1
3 162.12.0.123
4 this is the last line

匹配123,而且把含有123的行162都替换成172

[root@localhost ~]# nl file.txt | sed "/123/{s/162/172/g;q}"
1 wtmp begins Mon Feb 24 14:26:08 2014
2 192.168.0.1
3 172.12.0.123
4 this is the last line

这里大括号{}里能够运行多个命令,用;隔开就可以,q是退出

(8)连续编辑 -e

删除第二行,而且匹配把last替换成new

<pre name="code" class="plain">[root@localhost ~]# nl file.txt | sed -e "2d" -e "s/last/new/"
1 wtmp begins Mon Feb 24 14:26:08 2014
3 162.12.0.123
4 this is the new line

(9)直接改动文件,切记不要改动系统文件

[root@localhost ~]# sed -i "/begin/{s/24/25/g}" file.txt
[root@localhost ~]# cat file.txt
wtmp begins Mon Feb 25 14:26:08 2014
192.168.0.1
162.12.0.123
this is the last line

三 、一个比較有趣的样例

怎样替换\n也就是把全部的行都归为一行


第一种方式

[root@localhost ~]# sed ':a;N;$!ba;s/\n/ /g' file.txt
wtmp begins Mon Feb 25 14:26:08 2014 192.168.0.1 162.12.0.123 this is the last line

另外一种方式

[root@localhost ~]# tr "\n" " " < file.txt
wtmp begins Mon Feb 25 14:26:08 2014 192.168.0.1 162.12.0.123 this is the last line last linen

Linux Sed命令具体解释+怎样替换换行符&quot;\n&quot;(非常多面试问道)的更多相关文章

  1. Linux下Shell脚本替换换行符(转)

    说明:过滤换行符确实有点吭,不是那么好弄.处理文本时需要将换行符替换为空格,若使用sed命令会比较麻烦,而使用tr命令非常方便. 输入文本(country.txt) China America Fra ...

  2. linux sed命令参数及用法详解

    linux sed命令参数及用法详解 http://blog.csdn.net/namecyf/article/details/7336308 1. Sed简介 sed 是一种在线编辑器,它一次处理一 ...

  3. [转帖]linux sed命令

    linux sed命令就是这么简单 https://www.cnblogs.com/wangqiguo/p/6718512.html 用到的最多的就是一个sed -i 's/nn/mm/' 的命令了. ...

  4. 理解linux sed命令

    理解linux sed命令(2010-02-27 18:21:20) 标签:linuxshellsed替换 分类:革命本钱 1. Sed简介sed是一种在线编辑器,它一次处理一行内容.处理时,把当 前 ...

  5. sed替换换行符“\n”

    linux sed命令,如何替换换行符“\n” 在一次sed使用中,执行命令: sed "s/\n//g" file 1 发现,没起到任何效果. 后来,经查sed官方用户手册,才得 ...

  6. 【转】linux sed命令

    转自:linux sed命令就是这么简单 参考:Linux三大剑客之sed:https://blog.csdn.net/solaraceboy/article/details/79272344 阅读目 ...

  7. Linux ln命令具体解释及使用

    Linux ln命令具体解释及使用 ln是linux中一个很重要命令,它的功能是为某一个文件在另外一个位置建立一个不同的链接,这个命令最经常使用的參数是-s,详细使用方法是:ln –s 源文件 目标文 ...

  8. **PHP中替换换行符

    PHP中替换换行符 php 不同系统的换行不同系统之间换行的实现是不一样的linux 与unix中用 \nMAC 用 \rwindow 为了体现与linux不同 则是 \r\n所以在不同平台上 实现方 ...

  9. 替换换行符:回车换行CR/LF

    windows采用回车+换行CR/LF表示下一行,UNIX/Linux使用换行符LF表示下一行,MAC OS系统使用用回车符CR表示下一行. CR使用符号'\r'表示, ASCII码是13: LF使用 ...

随机推荐

  1. Codeforces Round #315 (Div. 2) B 水题强行set

    B. Inventory time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  2. Python 安装MySQLdb模块遇到报错及解决方案:_mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory

    一.问题 系统:win7 64位 在下载MySQL-python-1.2.5.zip,使用python setup.py install 安装时,出现以下报错: _mysql.c(42) : fata ...

  3. 微信小程序 body属性的问题

    微信小程序里面没有了body这个节点了,取而代之的是page

  4. pat 团体天梯 L3-003. 社交集群

    L3-003. 社交集群 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在社交网络平台注册时,用户通常会输入自己的兴趣爱好, ...

  5. Shell 环境中的输入输出重定向

    Linux Shell 环境中的输入输出重定向,用符号<和>来表示.0.1和2分别表示标准输入.标准输出和标准错误. 1.重定向标准输出到文件: cat fo > foo.txt 2 ...

  6. FCKeditor2.6.4控件及其使用范例

    原文发布时间为:2009-06-14 -- 来源于本人的百度文章 [由搬家工具导入] 已经精简了,很小,不多说了,我的源代码去下载看。 web.config 看看。。。还有bin里面的dll就是控件, ...

  7. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---30

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  8. LeetCode OJ-- Reorder List **

    https://oj.leetcode.com/problems/reorder-list/ 将一个链表重新排序,比如 1 2 3 4 5,变成 1 5 2 4 3 1.找到中间节点 mid 2.将链 ...

  9. LeetCode OJ-- Jump Game

    https://oj.leetcode.com/problems/jump-game/ 从0开始,根据每一位上存的数值往前跳. 这道题给想复杂了... 记录当前位置 pos,记录可以调到的最远达位置为 ...

  10. Codeforces 899 C.Dividing the numbers-规律

      C. Dividing the numbers   time limit per test 1 second memory limit per test 256 megabytes input s ...