Welcome 微信登录

首页 / 操作系统 / Linux / Linux sed常规用法小结

sed是一个非交互性的流编辑器,是stream editor的缩写。sed每次只处理一行内容,可以对文本或标准输入流进行处理。需要注意的是,sed并不直接操作初始数据,它操作的是一份原始数据的拷贝。sed处理时,把当前处理的行存储在临时缓冲区中,然后处理缓冲区中的内容,处理完成后,如果没有重定向到文件, 将把缓冲区中的内容送往屏幕,接着处理下一行直到处理完毕。理论的东东,这里不谈了,下面谈些常规应用吧。在演示之前,我们先构筑个初始文件orig.txt 和append.txt
  1. [root@localhost .shell]# cat orig.txt  
  2. id      name    price   address  
  3. --------------------------------     
  4. 1       apple   $50     USA  
  5. 2       pear    $60     Canda  
  6. 3       pen     $1      China      
  7. --------------------------------         
  8. total   $111  
  9. [root@localhost .shell]# cat append.txt   
  10. 4       apple   $50     USA  
  11. 5       $60     Canda  
  12. 6       pen     $1      China  
1、打印指定行 a)打印第一行
  1. [root@localhost .shell]# sed -n "1p" orig.txt   
  2. id      name    price   address  
b)打印3-5行
  1. [root@localhost .shell]# sed -n "3,5p" orig.txt   
  2. 1       apple   $50     USA  
  3. 2       pear    $60     Canda  
  4. 3       pen     $1      China  
c)打印最后一行
  1. [root@localhost .shell]# sed -n "$p" orig.txt   
  2. total   $111  
d)打印所有行
  1. [root@localhost .shell]# sed -n "1,$p" orig.txt   
  2. id      name    price   address  
  3. -------------------------------  
  4. 1       apple   $50     USA  
  5. 2       pear    $60     Canda  
  6. 3       pen     $1      China  
  7. --------------------------------  
  8. total   $111  
e)打印含有pen的行
  1. [root@localhost .shell]# sed -n "/pen/p" orig.txt   
  2. 3       pen     $1      China  
f)从第一行开始打印,打印到第一个含有$行
  1. [root@localhost .shell]# sed -n "1,/$/"p  orig.txt     
  2. id      name    price   address  
  3. -------------------------------  
  4. 1       apple   $50     USA  
  • 1
  • 2
  • 3
  • 4
  • 下一页
Linux应用基础教程笔记总结基于Linux2.6.38.8内核zImage文件的自解压详解相关资讯      Linux命令 
  • 48 字节命令可令 Linux 系统当机!  (今 07:55)
  • 在Linux笔记本上执行这句命令能致  (02月02日)
  • .NET开发必会的Linux命令  (12/20/2015 10:32:03)
  • 运维工程师必会的109个Linux命令  (03月09日)
  • 有趣的Linux命令行:随机输出唐诗  (12/28/2015 20:14:23)
  • 10 个 Linux 中的 passwd 命令示例  (10/29/2015 10:14:30)
本文评论 查看全部评论 (0)
表情: 姓名: 字数