Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

I have .csv files with the following structure:

cat,dog,mouse,pig,bird,cow,...
21,34,54,566,78,1,...
23,10,12,569,56,4,...
32,20,13,123,56,3,...
34,30,44,322,66,2,...

I want to filter the column related to the mouse, for instance:

54
12
13
44

How do I do it? Please keep in mind that I do not know in which column the mouse is found (my files are quite large, there are several files to filter, and the positions of the columns vary).

If I knew the exact position, I could use, for instance:

cat $file | awk '{printf("%s\n", $3);}' > filtered_file

What if I do not know the mouse is in column 3?

I really appreciate any help.

share|improve this question
add comment

marked as duplicate by Mat, Stephane Chazelas, vonbrand, MaxMackie, Gilles Mar 14 '13 at 23:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

You could do it like that:

#!/bin/bash

file=$1
column=$2
seperator=','

# Check if a csv file and a column name is given.
if [[ -z $file || -z $column ]]; then
  echo "Usage: $0 csvfile column"
  exit 1
fi

# Iterate through the first row and try to find the requested column.
field=1
for column_name in $(head -n 1 $file | tr $seperator ' '); do
  [[ $column_name == $column ]] && break
  field=$((field+1))
done

# Finally print the output.
cat $file | cut -d $seperator -f $field | sed "1d"

(Credits: I got the idea of how to get the first line from this post on stackoverflow and the idea of how to delete the first line from this post on unix.com).

share|improve this answer
    
It works perfectly, thank you very much! –  Bruno Mar 15 '13 at 13:26
add comment

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