2

I have CSV files which are in a form of:

1.csv
1,2,3,4,5,6,7,8
2.csv
2,4,5,7,8,8,9,5
3.csv
2,3,4,5,6,7,8,9

.....

up to 18.csv.

I want the result in result.csv to be:

1,2,3,4,5,6,7,8,2,4,5,7,8,8,9,5,2,3,4,5,6,7,8,9

I tried cat but it appends one another after only. I also want to transpose the CSV such that 1.csv is converted to:

1
2
3
4
5
6
7
8
1

I got the result.csv file with:

for i in {1..18}; do paste $i.csv; done | paste -s > result.csv

Or with cat:

for i in {1..18}; do cat $i.csv; done | paste -s > result.csv

(Put all together with paste/cat and then format the output with -s in order to have it in one line.)

Avoiding the loop:

cat {1..18}.csv | paste -s > result.csv

To transpose 1.csv:

sed 's/,/\n/g' 1.csv

(Just convert commas by carriage return)

| improve this answer | |
  • Is it a way to transpose my csv file? – RKR Jan 18 '17 at 12:24
  • sed 's/,/\n/g' 1.csv – Zumo de Vidrio Jan 18 '17 at 12:26
  • 2
    Note, instead of the for loop: cat {1..18}.csv – glenn jackman Jan 18 '17 at 17:25
0

Parte1:

paste -d, {1..18}.csv 

for generic transposition use datamash:

cat {1..3}.csv | datamash -t, transpose
1,2,2
2,4,3
3,5,4
4,7,5
5,8,6
6,8,7
7,9,8
8,5,9
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.