In the following example we are going to show how to remove columns from CSV file based on the column number. Consider a following linux command separated file containing 10 columns:
$ cat temp.csv 
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10

In the first example we are going to remove second column. The best tool for this job is cut command:
$ cut -d, -f2 --complement temp.csv 
1,3,4,5,6,7,8,9,10
1,3,4,5,6,7,8,9,10
1,3,4,5,6,7,8,9,10
1,3,4,5,6,7,8,9,10
1,3,4,5,6,7,8,9,10
1,3,4,5,6,7,8,9,10
1,3,4,5,6,7,8,9,10
Next, we will remove all columns in range 2-4 and 7,9:
$ cut -d, -f2-4,7-9 --complement temp.csv 
1,5,6,10
1,5,6,10
1,5,6,10
1,5,6,10
1,5,6,10
1,5,6,10
1,5,6,10
Remove the --complement option to remove columns 1,5,6,10:
$ cut -d, -f2-4,7-9 temp.csv 
2,3,4,7,8,9
2,3,4,7,8,9
2,3,4,7,8,9
2,3,4,7,8,9
2,3,4,7,8,9
2,3,4,7,8,9
2,3,4,7,8,9
In the last example we will remove columns 1,5,7 using bash variable:
$ remove='1,5,7'
$ cut -d, -f$remove --complement temp.csv 
2,3,4,6,8,9,10
2,3,4,6,8,9,10
2,3,4,6,8,9,10
2,3,4,6,8,9,10
2,3,4,6,8,9,10
2,3,4,6,8,9,10
2,3,4,6,8,9,10
FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.