How to Print All columns From the nth to the Last?

I often see a log where I want to print sequential columns in the log for a purpose.

 

Problem

How to print all columns from the nth to the last?

 

Solution

For example, you have a test.txt file that contains the following:

No  Name        Address
1   Richard     Apt. 344 86094 Swaniawski Drive, East Suzetteshire, MT 51323-2013
2   Alex        4522 Rosenbaum Island, Lake Suzan, IL 68193
3   Bryan       Apt. 907 703 Douglas Run, West Brainburgh, MT 70080-8990

 

Display the log

 

Usually, to display the complete Address column, I will run the command below:

awk '{print $3,$4,$5,$6,$7,$8,$9,$10,$11,$12}' test.txt 

Using the original script

 

However, I think this method is less effective because if the address is more than 12 columns, then I have to write more than 12 items, and it is very tiring. After searching on the internet, there are 2 methods you can use:

1. Using the awk command

From the test.txt file, you just want to print the entire column except columns 1 and 2, then you can use the command below:

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

 

The result will be as shown in the image below:

print all columns from the nth to the last
Using the awk command

 

2. Using the cut command

In addition to using awk, you can also use the cut command to display the same by using the command below:

cut -d' ' -f3- test.txt 

 

And the result will be as shown in the image below when you run the command above:

print all columns from the nth to the last
Using the cut command

 

Warning
Replace the variable -f3- with what column you will start printing. If you start printing from column 7, then change the command above to the following:

cut -d" " -f7- test.txt

 

Note

There are still several methods to print columns that run from the nth to the last column, but I think those two methods will suffice.

References

testingbot.com
stackoverflow.com