linux中如何删除文本开头的多个空格和tab键

 

1、测试数据

[root@centos7 test]# ls
test.txt
[root@centos7 test]# cat test.txt     ## 测试数据
         y      j k
                j k     h
                d                j r
[root@centos7 test]# sed -n l test.txt
         y\tj k$
\t\tj k\th$
  \t\td\t\t j r$

 

2、

[root@centos7 test]# ls
test.txt
[root@centos7 test]# cat test.txt
         y      j k
                j k     h
                d                j r
[root@centos7 test]# sed -n l test.txt
         y\tj k$
\t\tj k\th$
  \t\td\t\t j r$
[root@centos7 test]# sed 's/^[\t ]\+//' test.txt   ## 删除行首空格和制表符
y       j k
j k     h
d                j r

 

3、

[root@centos7 test]# ls
test.txt
[root@centos7 test]# cat test.txt
         y      j k
                j k     h
                d                j r
[root@centos7 test]# sed -n l test.txt
         y\tj k$
\t\tj k\th$
  \t\td\t\t j r$
[root@centos7 test]# sed 's/^[\t ]*//' test.txt
y       j k
j k     h
d                j r

 

4、

[root@centos7 test]# ls
test.txt
[root@centos7 test]# cat test.txt
         y      j k
                j k     h
                d                j r
[root@centos7 test]# sed -n l test.txt
         y\tj k$
\t\tj k\th$
  \t\td\t\t j r$
[root@centos7 test]# sed 's/^\s\+//' test.txt
y       j k
j k     h
d                j r

 

5、

[root@centos7 test]# ls
test.txt
[root@centos7 test]# cat test.txt
         y      j k
                j k     h
                d                j r
[root@centos7 test]# sed -n l test.txt
         y\tj k$
\t\tj k\th$
  \t\td\t\t j r$
[root@centos7 test]# sed 's/^\s*//' test.txt
y       j k
j k     h
d                j r

 

posted @ 2022-04-09 14:25  小鲨鱼2018  阅读(599)  评论(0编辑  收藏  举报