3

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

2 Answers 2

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)

3
  • Is it a way to transpose my csv file?
    – RKR
    Commented Jan 18, 2017 at 12:24
  • sed 's/,/\n/g' 1.csv Commented Jan 18, 2017 at 12:26
  • 2
    Note, instead of the for loop: cat {1..18}.csv Commented Jan 18, 2017 at 17:25
1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .