1. 程式人生 > 其它 >linux中如何刪除文字開頭的多個空格和tab鍵

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