Dave
Dave

Reputation: 19110

How do I write a shell command to extract the second and second to last columns of a CSV file?

I'm using bash shell on Mac Yosemite. I found this command for extracting the second column of a CSV file ...

awk -F "\"*,\"*" '{print $2}'

but my quesiton is what if I want to extract both the second and second-to-last column? For simplicity, we can assume there is more than one column in the CSV data.

Upvotes: 1

Views: 119

Answers (2)

Vijay S B
Vijay S B

Reputation: 1315

This also works

    awk -F , '{print $2,$(NF-1)}' temp1.csv

Upvotes: 0

anubhava
anubhava

Reputation: 784998

You can use $(NF-1) to get second-to-last column:

awk -F '"?,"?' '{print $2, $(NF-1)}' file.csv

Upvotes: 4

Related Questions