Linux sed常规用法小结

本文介绍了sed流编辑器的基本使用方法,包括打印指定行、替换文本、删除行、添加行以及导出文件等常见操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sed是一个非交互性的流编辑器,是stream editor的缩写。sed每次只处理一行内容,可以对文本或标准输入流进行处理。需要注意的是,sed并不直接操作初始数据,它操作的是一份原始数据的拷贝。sed处理时,把当前处理的行存储在临时缓冲区中,然后处理缓冲区中的内容,处理完成后,如果没有重定向到文件, 将把缓冲区中的内容送往屏幕,接着处理下一行直到处理完毕。
理论的东东,这里不谈了,下面谈些常规应用吧。
在演示之前,我们先构筑个初始文件orig.txt 和append.txt

[plain]  view plain copy
  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)打印第一行

[plain]  view plain copy
  1. [root@localhost .shell]# sed -n '1p' orig.txt   
  2. id      name    price   address  

b)打印3-5行

[plain]  view plain copy
  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)打印最后一行

[plain]  view plain copy
  1. [root@localhost .shell]# sed -n '$p' orig.txt   
  2. total   $111  

d)打印所有行

[plain]  view plain copy
  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的行

[plain]  view plain copy
  1. [root@localhost .shell]# sed -n '/pen/p' orig.txt   
  2. 3       pen     $1      China  

f)从第一行开始打印,打印到第一个含有$行

[plain]  view plain copy
  1. [root@localhost .shell]# sed -n '1,/\$/'p  orig.txt     
  2. id      name    price   address  
  3. -------------------------------  
  4. 1       apple   $50     USA  


2、打印含有元数据$行的行号

[plain]  view plain copy
  1. [root@localhost .shell]# sed -n '/\$/=' orig.txt            
  2. 3  
  3. 4  
  4. 5  
  5. 7  

3、替换

a)把-替换为空

[plain]  view plain copy
  1. [root@localhost .shell]# sed -e 's/-*//g'  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  

b)把元数据$替换为¥

[plain]  view plain copy
  1. [root@localhost .shell]# sed 's/\$/¥/g' 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  

c)-替换为空,并删除空行

[plain]  view plain copy
  1. [root@localhost .shell]# sed -e 's/-*//g'  -e '/^$/'d orig.txt   
  2. id      name    price   address  
  3. 1       apple   $50     USA  
  4. 2       pear    $60     Canda  
  5. 3       pen     $1      China  
  6. total   $111  

d)把含有total的行,替换为******

[plain]  view plain copy
  1. [root@localhost .shell]# sed  '/total/c\******' 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. ******  

4、删除,删除前两行

[plain]  view plain copy
  1. [root@localhost .shell]# sed '1,2d' orig.txt   
  2. 1       apple   $50     USA  
  3. 2       pear    $60     Canda  
  4. 3       pen     $1      China  
  5. --------------------------------  
  6. total   $111  

5、添加

a)在含有apple的行前插入一行 0       nothing   $5     USA

[plain]  view plain copy
  1. [root@localhost .shell]# sed '/apple/i\0       nothing   $5     USA' orig.txt    
  2. id      name    price   address  
  3. -------------------------------  
  4. 0       nothing   $5     USA  
  5. 1       apple   $50     USA  
  6. 2       pear    $60     Canda  
  7. 3       pen     $1      China  
  8. --------------------------------  

b)在含有pen的行后插入一行4       four   $5     USA

[plain]  view plain copy
  1. [root@localhost .shell]# sed '/pen/a\4       four   $5     USA' orig.txt                 
  2. id      name    price   address  
  3. -------------------------------  
  4. 1       apple   $50     USA  
  5. 2       pear    $60     Canda  
  6. 3       pen     $1      China  
  7. 4       four   $5     USA  
  8. --------------------------------  
  9. total   $111  

c)在含有pen的行后面追加append.txt 文件的内容

[plain]  view plain copy
  1. [root@localhost .shell]# sed '/pen/r append.txt' orig.txt   
  2. id      name    price   address  
  3. -------------------------------  
  4. 1       apple   $50     USA  
  5. 2       pear    $60     Canda  
  6. 3       pen     $1      China  
  7. 4       apple   $50     USA  
  8. 5       $60     Canda  
  9. 6       pen     $1      China  
  10. --------------------------------  
  11. total   $111  

6、导出文件

a)把含有$的行导出到out.txt文件中

[plain]  view plain copy
  1. [root@localhost .shell]# sed -n '/\$/w out.txt' orig.txt   
  2. [root@localhost .shell]# cat out.txt                        
  3. 1       apple   $50     USA  
  4. 2       pear    $60     Canda  
  5. 3       pen     $1      China  
  6. total   $111  

b)把含有apple的行输出到apple.txt文件中

[plain]  view plain copy
  1. [root@localhost .shell]# sed -n '/apple/'p orig.txt > apple.txt  
  2. [root@localhost .shell]# cat apple.txt   
  3. 1       apple   $50     USA  
转载自: http://blog.csdn.net/love__coder/article/details/7024739
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值