I have multiple .csv files and I want to extract a particular column from each. Lets say column 5. I want to take that column and add it to a csv file and append a new column onto it from each successive file. I am able to do this with the following code taken from someone else:
awk '{_[FNR]=(_[FNR] OFS $1)}END{for (i=1; i<=FNR; i++) {sub(/^ /,"",_[i]); print _[i]}}' input*.csv > output.csv`
When I look at the output file I notice that the order in which the columns is added isn't sequential. As a result I was hoping to modify the code so that the header of the column is the filename from which the column came. How can I go about doing this?
For example: input1.csv can be:
1,2,3,4,5
6,7,8,9,10
input2.csv could be:
11,12,13,14,15
16,17,18,19,20
And I would want the output.csv to be:
input1.csv, input2.csv
5,15
10,20
I hope this makes sense and thanks in advance.