[Linux] awk wants to print all the columns after the specified column

Today, I happened to study awk. There is a file. The content of the file is 1 2 3 4 5 6 7 8 9 0

Now you want to print all columns except the first one

Document content:

[root@localhost ~]# cat test.txt
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0

Execution code:

awk '{ $1=""; print $0 }' test.txt 

The results of the implementation are:

[root@localhost ~]# awk '{ $1=""; print $0 }' test.txt 
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0

I found that the first row was missing

But if you want to cut down the second column, you can change $1 to $2

[root@localhost ~]# awk '{ $2=""; print $0 }' test.txt 
1  3 4 5 6 7 8 9 0
1  3 4 5 6 7 8 9 0
1  3 4 5 6 7 8 9 0
1  3 4 5 6 7 8 9 0
1  3 4 5 6 7 8 9 0
1  3 4 5 6 7 8 9 0
1  3 4 5 6 7 8 9 0
1  3 4 5 6 7 8 9 0
1  3 4 5 6 7 8 9 0
1  3 4 5 6 7 8 9 0
1  3 4 5 6 7 8 9 0
1  3 4 5 6 7 8 9 0
1  3 4 5 6 7 8 9 0
1  3 4 5 6 7 8 9 0

Similarly, in the last column, you can change $2 to $NF

[root@localhost ~]# awk '{ $NF=""; print $0 }' test.txt 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 

Another way is to write a loop in awk

[root@localhost ~]# awk '{ for(i=1; i<=1; i++){ $i="" }; print $0 }' test.txt 
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0
 2 3 4 5 6 7 8 9 0

Again, if you want to output $6 to the end, you can write like this

[root@localhost ~]# awk '{ for(i=1; i<=5; i++){ $i="" }; print $0 }' test.txt 
     6 7 8 9 0
     6 7 8 9 0
     6 7 8 9 0
     6 7 8 9 0
     6 7 8 9 0
     6 7 8 9 0
     6 7 8 9 0
     6 7 8 9 0
     6 7 8 9 0
     6 7 8 9 0
     6 7 8 9 0
     6 7 8 9 0
     6 7 8 9 0
     6 7 8 9 0

 

Posted by diego25 on Wed, 18 Dec 2019 07:04:18 -0800