linux中如何将多个连续的tab键替换为一个tab键

 

 

1、测试数据

复制代码
[root@centos7 test]# ls
test.txt
[root@centos7 test]# cat test.txt   ## 测试数据
k                       m
w               g                       h
g       u       y
[root@centos7 test]# sed -n l test.txt   ## 查看tab键
k\t\t\tm$
w\t\tg\t\t\th$
g\tu\ty$
复制代码

 

2、将多个tab键替换为一个tab键

复制代码
[root@centos7 test]# ls
test.txt
[root@centos7 test]# sed -n l test.txt
k\t\t\tm$
w\t\tg\t\t\th$
g\tu\ty$
[root@centos7 test]# sed 's/[\t]\+/\t/' test.txt | sed -n l
k\tm$
w\tg\t\t\th$
g\tu\ty$
[root@centos7 test]# sed 's/[\t]\+/\t/g' test.txt | sed -n l   ## 将多个连续的tab键替换为一个tab键
k\tm$
w\tg\th$
g\tu\ty$
复制代码

 

3、

复制代码
[root@centos7 test]# ls
test.txt
[root@centos7 test]# cat test.txt
k                               m
w               g                       h
g       u                       y
[root@centos7 test]# sed -n l test.txt
k\t\t\t\tm$
w\t\tg\t\t\th$
g       u\t\t\ty$
[root@centos7 test]# sed 's/\t\+/\t/' test.txt | sed -n l
k\tm$
w\tg\t\t\th$
g       u\ty$
[root@centos7 test]# sed 's/\t\+/\t/g' test.txt | sed -n l
k\tm$
w\tg\th$
g       u\ty$
复制代码

 

4、\s: 同时代表空格和tab键

复制代码
[root@centos7 test]# ls
test.txt
[root@centos7 test]# cat test.txt
k                               m
w               g                       h
g       u                       y
[root@centos7 test]# sed -n l test.txt
k\t\t\t\tm$
w\t\tg\t\t\th$
g       u\t\t\ty$
[root@centos7 test]# sed 's/\s\+/\t/' test.txt | sed -n l
k\tm$
w\tg\t\t\th$
g\tu\t\t\ty$
[root@centos7 test]# sed 's/\s\+/\t/g' test.txt | sed -n l
k\tm$
w\tg\th$
g\tu\ty$
复制代码

 

posted @   小鲨鱼2018  阅读(615)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示