How to extract columns from one file and move to another file without printing the columns?

I have a 2GB csv file called data.csv. I want to extract 5 columns from data.csv and move to a new file (preferably a new csv file).

How can I do that? Is there an awk command which allows me to do that without actually printing the columns?

Asked By: ilkayn

||

Something along the lines of:

csv 1 3 4 6 7 <data.csv >new.csv
  • assuming columns 1 3 4 6 and 7 of course! For RHEL and clones the command csv is in EPEL and can be installed in the usual way. For Ubuntu and its friends try:
csvtool col 1,3,4,6,7 data.csv >new.csv

For example:

$ cat data.csv
1,2,3,4,5,6,7,8,9,0
q,w,e,r,t,y,u,i,o,p
a,s,d,f,g,h,j,k,l
z,x,c,v,b,n,m
$ csvtool col 1,2,3,4,5 data.csv >new.csv
$ cat new.csv 
1,2,3,4,5
q,w,e,r,t
a,s,d,f,g
z,x,c,v,b

instead.

Answered By: Martin

For that simple case without any csv specific extravagances, cut might do:

cut -d, -f1-5 file
1,2,3,4,5
q,w,e,r,t
a,s,d,f,g
z,x,c,v,b

or, for single non-contiguous fields,

cut -d, -f1,3,7 file
1,3,7
q,e,u
a,d,j
z,c,m

Redirect to the desired new .csv file.

Answered By: RudiC
Categories: Answers Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.