How can I find and filter a specific column in a .csv file?

6,698

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:
6,698

Related videos on Youtube

Bruno
Author by

Bruno

Updated on September 18, 2022

Comments

  • Bruno
    Bruno almost 2 years

    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.