Linux Sed命令详解+如何替换换行符"\n"(很多面试问道)

来源:互联网 发布:十六进制转十进制算法 编辑:程序博客网 时间:2024/05/02 11:23
Sed
Sed是一个强大的文本处理工具
可以采用正则匹配,对文本进行插入删除修改等操作
Sed处理的时候,一次处理一行,每一次把当前处理的存放在临时缓冲区,处理完后输出缓冲区内容到屏幕,然后把下一行读入缓冲区,如此重复,直到结尾。


1、命令格式和参数
sed [-nefr] [动作] 文件
参数:
-n 安静模式,在sed处理的时候,所有来自STDIN的数据都会被输出到终端,加上-n会只输出处理的哪行
-e 直接在命令列上进行sed动作编辑
-f 直接将sed的动作写在文件内
-r sed动作支持延伸的正则表达(默认只是基础正则)
-i 直接修改文件内容(慎用,尤其是用系统文件做练习的时候)


动作:
append:增加,在当前行的下一行增加
c  :取代,取代n1到n2之间的行
ddelete:删除
i插入,目前行的上一行插入
p打印,常常与-n使用
s取代,s/old/new/g


2、基础用法详解
(1)第一行之后添加一行

[plain] view plain copy
  1. [root@localhost ~]# nl file.txt | sed "1a add text"  
  2.      1  wtmp begins Mon Feb 24 14:26:08 2014  
  3. add text  
  4.      2  192.168.0.1  
  5.      3  162.12.0.123  
  6.      4  this is the last line  
(2)第一行之前添加一行
[plain] view plain copy
  1. [root@localhost ~]# nl file.txt | sed "1i add text"  
  2. add text  
  3.      1  wtmp begins Mon Feb 24 14:26:08 2014  
  4.      2  192.168.0.1  
  5.      3  162.12.0.123  
  6.      4  this is the last line  
(3)删除第2,3行
[plain] view plain copy
  1. [root@localhost ~]# nl file.txt | sed "2,3d"  
  2.      1  wtmp begins Mon Feb 24 14:26:08 2014  
  3.      4  this is the last line  
(4)打印第2,3行
[plain] view plain copy
  1. [root@localhost ~]# sed -n "2,3p" file.txt   
  2. 192.168.0.1  
  3. 162.12.0.123  

这里要提到的是,尽量使用-n,不然会出现这样的结果
[plain] view plain copy
  1. [root@localhost ~]# sed "2,3p" file.txt   
  2. wtmp begins Mon Feb 24 14:26:08 2014  
  3. 192.168.0.1  
  4. 192.168.0.1  
  5. 162.12.0.123  
  6. 162.12.0.123  
  7. this is the last line  

(5)把168换成169
先看源文件
[plain] view plain copy
  1. [root@localhost ~]# cat file.txt   
  2. wtmp begins Mon Feb 24 14:26:08 2014  
  3. 192.168.0.1  
  4. 162.12.0.123  
  5. this is the last line  
处理后
[plain] view plain copy
  1. [root@localhost ~]# sed "s/168/169/g" file.txt   
  2. wtmp begins Mon Feb 24 14:26:08 2014  
  3. 192.169.0.1  
  4. 162.12.0.123  
  5. this is the last line  

(6)插入多行
[plain] view plain copy
  1. [root@localhost ~]# nl file.txt | sed "2afirst\nsecond" file.txt   
  2. wtmp begins Mon Feb 24 14:26:08 2014  
  3. 192.168.0.1  
  4. first  
  5. second  
  6. 162.12.0.123  
  7. this is the last line  

(7)匹配数据,然后进行操作
只需要在上述的基础上加上正则匹配
sed "/匹配的模式/处理的方式" file.txt 
sed "/^root/d" file.txt 对开始有root的删除
例如
匹配begin,并删除改行
[plain] view plain copy
  1. [root@localhost ~]# nl file.txt | sed "/begin/d"  
  2.      2  192.168.0.1  
  3.      3  162.12.0.123  
  4.      4  this is the last line  
匹配123,并且把含有123的行162都替换成172
[plain] view plain copy
  1. [root@localhost ~]# nl file.txt | sed "/123/{s/162/172/g;q}"  
  2.      1  wtmp begins Mon Feb 24 14:26:08 2014  
  3.      2  192.168.0.1  
  4.      3  172.12.0.123  
  5.      4  this is the last line  
这里大括号{}里可以执行多个命令,用;隔开即可,q是退出
(8)连续编辑 -e
删除第二行,并且匹配把last替换成new
[plain] view plain copy
  1. <pre name="code" class="plain">[root@localhost ~]# nl file.txt | sed -e "2d" -e "s/last/new/"  
  2.      1  wtmp begins Mon Feb 24 14:26:08 2014  
  3.      3  162.12.0.123  
  4.      4  this is the new line  

(9)直接修改文件,切记不要修改系统文件
[plain] view plain copy
  1. [root@localhost ~]# sed -i "/begin/{s/24/25/g}" file.txt   
  2. [root@localhost ~]# cat file.txt   
  3. wtmp begins Mon Feb 25 14:26:08 2014  
  4. 192.168.0.1  
  5. 162.12.0.123  
  6. this is the last line  


三 、一个比较有趣的例子
如何替换\n也就是把所有的行都归为一行

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

第二种方式

[plain] view plain copy
  1. [root@localhost ~]# tr "\n" " " < file.txt   
  2. wtmp begins Mon Feb 25 14:26:08 2014 192.168.0.1 162.12.0.123 this is the last line last linen  
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 头七家里有狗怎么办 股票退市股民的钱怎么办 美国股票退市股民怎么办 百度云字幕和视频不同步怎么办 百度云加载字幕有延迟怎么办 很难适应新环境怎么办 蜘蛛丝碰到嘴唇上起包有毒怎么办? 电瓶车在路上爆胎了怎么办 嘴被虫子咬肿了怎么办 高铁管家购票失败怎么办 高铁车厢空调冷怎么办 高铁票过了时间怎么办 网购火车票丢了怎么办 改签没有票了怎么办 火车票取了没赶上车怎么办 上车后车票丢了怎么办 晒了吗任务过期怎么办 坐火车买了站票怎么办 坐火车忘记带票怎么办 距离二本线差几分怎么办 行李包落火车候车厅怎么办 高铁票买错地点怎么办 高铁票买错日期怎么办 票买错时间了怎么办 上高铁了票丢了怎么办 上车前高铁票丢了怎么办 高铁安检没收的东西怎么办 高铁安检员老了怎么办 高铁安检喷雾拍照了怎么办 十个小时的高铁怎么办 华为开机需要激活码怎么办 高铁提前上车了出站怎么办 买火车票忘记带身份证怎么办 买高铁票没赶上怎么办 电脑放视频没有声音怎么办 内业计算中角度超限怎么办 遇到飞机出故障乘客怎么办 模拟城市5矿产满了怎么办 模拟城市5劳工短缺怎么办 模拟城市5土地价值低怎么办 考上研究生想二战研究生学籍怎么办