Suppose I have a crazy data set in CSV:

OH0012781511,87,26953,HOUSEHOLDER,SHERRY,LEIGH,,11/26/1965,08/19/1988,,211 N GARFIELD ST ,   ,BLOOMDALE,OH,44817,,,,  PO BOX 222  ,   ,BLOOMDALE,OH,44817,,,,PENTA JVSD,,,,05,06,WOOD EDUC SRV CTR,,,ELMWOOD LOCAL SD,BOWLING-GREEN,BLOOM TWP BLOOMDALE,87-P-ABO,02,03,02,,BLOOMDALE VILLAGE,,D,,,X,,X,,,,X,,,,,,,,,,,,,D,,X,,,,,,X,,,,,,,,,X,,,,,,,
OH0012781528,87,31122,KEATON,JENNIFER,KAI,,11/27/1968,07/13/2015,,110 N GARFIELD ST ,   ,BLOOMDALE,OH,44817,,,,  PO BOX 16  ,   ,BLOOMDALE,OH,44817,,,,PENTA JVSD,,,,05,06,WOOD EDUC SRV CTR,,,ELMWOOD LOCAL SD,BOWLING-GREEN,BLOOM TWP BLOOMDALE,87-P-ABO,02,03,02,,BLOOMDALE VILLAGE,,,X,,,,,,,,X,,,,,,,,,,,,,,,X,,,,,,X,,,,,,,X,,X,,,,,,,

And I wish only to save the city and zip columns; Is there a simple command line spell, that I can specify the two names as arguments and save only those two columns in a new CSV file?


If you have simple, comma-separated columns, you can do it with awk. Parse the first line to determine the columns you want, and then print those columns.

wanted_columns=",$wanted_columns," awk -F, '
    NR==1 {
        for (i=1; i<=NF; i++) {
            if (index(ENVIRON["wanted_columns"], ","$i",")) {last=i; columns[i]=","}
        for (i=1; i<=NF; i++) {
            if (columns[i]) printf "%s%s", $i, columns[i]

Alternatively, you can use cut; this will be faster if the file is large. First parse the header line with different tools to determine the column numbers.

  IFS= read header;
  cut_spec=$(printf %s "$header" |
             wanted_columns=",$wanted_columns," awk -v RS=, '
                 index(ENVIRON["wanted_columns"], ","$0",") {printf "%d,", NR}'
  { printf %s\\n "$header"; cat; } | cut -d , -f "${cut_spec%,}";

If your CSV file is an actual CSV file with some quoted columns that can have embedded columns or newlines, use a proper CSV tool. For example, in Python:

#!/usr/bin/env python2
import csv, sys
wanted_columns = set(sys.argv[1:])
reader = csv.reader(sys.stdin)
header = reader.next()
columns = [i for i in range(len(header)) if header[i] in wanted_columns]
for row in reader: writer.writerow([row[i] for i in columns])

(Pass the desired column names as arguments to the script.)


If you don't have embedded , (meaning no ...,"foo,bar",...) you can use cut:

cut -d, -f13,15

selects only columns 13 and 15 where columns are delimited by ,. Why are columns 13 and 15 the right ones? I counted the commas with

head -n 1 data.csv | sed 's/zip,.*/,/' | tr -dc , | wc -c

Explanation: take the first line of input (head), find "zip" and replace it and the rest of the line with a comma (sed), then throw away everthing that is not a comma (tr) and count (wc). So "a,zip,b" is transformed to "a,,", to ",," to 2. "zip" is the second field.

  • The poster specified columns by name, I'm guessing they don't want to use column numbers. – forquare Sep 6 '15 at 18:43
  • 1
    Sure, but the poster didn't say explicitly what selection by number wouldn't be ok. – Thomas Erker Sep 6 '15 at 18:46
  • It is explicitly written that I can specify the two names as arguments, it is unreasonable to assume that someone is going to list all the ways he does not want the problem to be solved (assuming that exhaustively listing them is even possible), since he clearly indicated in which way he wanted it to be. – To마SE Sep 9 '15 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.