sed删除行首和行尾空格或者TAB

本文深入探讨了Bash命令sed的高级应用技巧,包括删除文本中的行首空格、tab键和行尾空格、tab键等内容,并通过具体实例展示了如何进行这些操作,帮助开发者更高效地处理文本文件。

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

实例:

liujl@liujl-ThinkPad-Edge-E431:~/mybash/test$ cat seddlt 
    1、I am eagerly awaiting my next disappointment. —Ashleigh Brilliant    
 2、Every man’s memory is his private literature. —Aldous Huxley    
  3、Life is what happens to you while you’re busy making other plans. —John Lennon    
 4、Life is really simple, but we insist on making it complicated. —Confucius    
  5、Do not dwell in the past, do not dream of the future, concentrate the mind on the    
    6、present moment. —Buddha    
 7、The more decisions that you are forced to make alone, the more you are aware of    
 8、your freedom to choose. —Thornton Wilder    

1、删除行头空格键和tab键

liujl@liujl-ThinkPad-Edge-E431:~/mybash/test$ cat seddlt | sed 's/^[ \t]*//g'
1、I am eagerly awaiting my next disappointment. —Ashleigh Brilliant    
2、Every man’s memory is his private literature. —Aldous Huxley    
3、Life is what happens to you while you’re busy making other plans. —John Lennon    
4、Life is really simple, but we insist on making it complicated. —Confucius    
5、Do not dwell in the past, do not dream of the future, concentrate the mind on the    
6、present moment. —Buddha    
7、The more decisions that you are forced to make alone, the more you are aware of    
8、your freedom to choose. —Thornton Wilder  
说明:

1) ^[  \t],是以空格或者tab键开头;与【^】不同,【^】是匹配除【^字符】之外的任意字符,例如5【^12】,匹配50、53、54,但是不匹配51和52

2)*是代表匹配0到多次


2、删除行尾空格和tab

liujl@liujl-ThinkPad-Edge-E431:~/mybash/test$ cat seddlt | sed 's/[ \t]*$//g'
    1、I am eagerly awaiting my next disappointment. —Ashleigh Brilliant
 2、Every man’s memory is his private literature. —Aldous Huxley
  3、Life is what happens to you while you’re busy making other plans. —John Lennon
 4、Life is really simple, but we insist on making it complicated. —Confucius
  5、Do not dwell in the past, do not dream of the future, concentrate the mind on the
    6、present moment. —Buddha
 7、The more decisions that you are forced to make alone, the more you are aware of
 8、your freedom to choose. —Thornton Wilder

说明:

$:行尾标志。

更多sed操作,请参见本博客

sed 简明教程

sed学习实例(一)



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值