Search code examples
linuxcsvcommand-linelarge-files

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?


Solution

  • 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