1. 程式人生 > 其它 >linux系統中awk命令刪除指定列

linux系統中awk命令刪除指定列

1、測試資料

[root@centos79 test3]# cat a.txt
3 5 6 2
s g 3 5
c f h e

2、刪除第二列

[root@centos79 test3]# cat a.txt
3 5 6 2
s g 3 5
c f h e
[root@centos79 test3]# awk '{$2 = ""; print $0}' a.txt  ## 刪除之後,多出一個空格
3  6 2
s  3 5
c  h e
[root@centos79 test3]# awk '{$2 = ""; print $0}' a.txt | cat -A
3  6 2$
s  3 5
$ c h e$

3、刪除第二列,不留空格

[root@centos79 test3]# cat a.txt
3 5 6 2
s g 3 5
c f h e
[root@centos79 test3]# awk '{$2 = "\b"; print $0}' a.txt
3 6 2
s 3 5
c h e

4、是否適用tab

[root@centos79 test3]# cat a.txt
3 5 6 2
s g 3 5
c f h e
[root@centos79 test3]# awk '{OFS = "\t"}{$2 = "\b"; print $0}' a.txt
3       6
2 s 3 5 c h e

5、是否適用tab

[root@centos79 test3]# cat a.txt
3 5 6 2
s g 3 5
c f h e
[root@centos79 test3]# sed 's/ /\t/g' a.txt -i
[root@centos79 test3]# cat a.txt
3       5       6       2
s       g       3       5
c       f       h       e
[root@centos79 test3]# awk '{OFS = "\t"}{$2 = "\b"; print $0}
' a.txt
3 6 2 s 3 5 c h e [root@centos79 test3]# awk '{$2 = "\b"; print $0}' a.txt 3 6 2 s 3 5 c h e