Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the second column of a very large csv file using linux command?

I was given this question during an interview. I said I could do it with java or python like xreadlines() function to traverse the whole file and fetch the column, but the interviewer wanted me to just use linux cmd. How can I achieve that?

like image 723
Pythoner Avatar asked May 11 '16 02:05

Pythoner


2 Answers

You can use the command awk.

Below is an example of printing out the second column of a file:

awk -F, '{print $2}' file.txt

And to store it, you redirect it into a file:

awk -F, '{print $2}' file.txt > output.txt
like image 87
Andreas DM Avatar answered Oct 06 '22 14:10

Andreas DM


You can use cut:

cut -d, -f2 /path/to/csv/file
like image 2
davlet Avatar answered Oct 06 '22 14:10

davlet