Skip to main content
Improved formatting. Minor fixes.
Source Link
Pablo A
  • 3.3k
  • 1
  • 26
  • 46

CSV parsing is not easily done with POSIX tools only, unless you are using a simplified CSV variant with no quoting (so that commas can't appear in a field). Even then, this task doesn't seem easy to do with awkawk or other text processing to tool. You can use Perl with Text::CSV, Python with csv, R with read.csv, Ruby with CSV, … (All of these are part of the standard library of the respective language except for Perl.)

For example, in Python:

import csv, sys rows = list(csv.reader(sys.stdin)) writer = csv.writer(sys.stdout) for col in xrange(0, len(rows[0])): writer.writerow([row[col] for row in rows]) 
import csv, sys rows = list(csv.reader(sys.stdin)) writer = csv.writer(sys.stdout) for col in xrange(0, len(rows[0])): writer.writerow([row[col] for row in rows]) 

CSV parsing is not easily done with POSIX tools only, unless you are using a simplified CSV variant with no quoting (so that commas can't appear in a field). Even then this task doesn't seem easy to do with awk or other text processing to tool. You can use Perl with Text::CSV, Python with csv, R with read.csv, Ruby with CSV, … (All of these are part of the standard library of the respective language except for Perl.)

For example, in Python:

import csv, sys rows = list(csv.reader(sys.stdin)) writer = csv.writer(sys.stdout) for col in xrange(0, len(rows[0])): writer.writerow([row[col] for row in rows]) 

CSV parsing is not easily done with POSIX tools only, unless you are using a simplified CSV variant with no quoting (so that commas can't appear in a field). Even then, this task doesn't seem easy to do with awk or other text processing to tool. You can use Perl with Text::CSV, Python with csv, R with read.csv, Ruby with CSV, … (All of these are part of the standard library of the respective language except for Perl.)

For example, in Python:

import csv, sys rows = list(csv.reader(sys.stdin)) writer = csv.writer(sys.stdout) for col in xrange(0, len(rows[0])): writer.writerow([row[col] for row in rows]) 
changed len(rows) to len(rows[0]) to iterate over all the columns, which is the intention of the code; renamed j to col to get past the 6 char minimum edit
Source Link

CSV parsing is not easily done with POSIX tools only, unless you are using a simplified CSV variant with no quoting (so that commas can't appear in a field). Even then this task doesn't seem easy to do with awk or other text processing to tool. You can use Perl with Text::CSV, Python with csv, R with read.csv, Ruby with CSV, … (All of these are part of the standard library of the respective language except for Perl.)

For example, in Python:

import csv, sys rows = list(csv.reader(sys.stdin)) writer = csv.writer(sys.stdout) for jcol in xrange(0, len(rowsrows[0])): writer.writerow([row[j][row[col] for row in rows]) 

CSV parsing is not easily done with POSIX tools only, unless you are using a simplified CSV variant with no quoting (so that commas can't appear in a field). Even then this task doesn't seem easy to do with awk or other text processing to tool. You can use Perl with Text::CSV, Python with csv, R with read.csv, Ruby with CSV, … (All of these are part of the standard library of the respective language except for Perl.)

For example, in Python:

import csv, sys rows = list(csv.reader(sys.stdin)) writer = csv.writer(sys.stdout) for j in xrange(0, len(rows)): writer.writerow([row[j] for row in rows]) 

CSV parsing is not easily done with POSIX tools only, unless you are using a simplified CSV variant with no quoting (so that commas can't appear in a field). Even then this task doesn't seem easy to do with awk or other text processing to tool. You can use Perl with Text::CSV, Python with csv, R with read.csv, Ruby with CSV, … (All of these are part of the standard library of the respective language except for Perl.)

For example, in Python:

import csv, sys rows = list(csv.reader(sys.stdin)) writer = csv.writer(sys.stdout) for col in xrange(0, len(rows[0])): writer.writerow([row[col] for row in rows]) 
code that actually works
Source Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k

CSV parsing is not easily done with POSIX tools only, unless you are using a simplified CSV variant with no quoting (so that commas can't appear in a field). Even then this task doesn't seem easy to do with awk or other text processing to tool. You can use Perl with Text::CSV, Python with csv, R with read.csv, Ruby with CSV, … (All of these are part of the standard library of the respective language except for Perl.)

For example, in Python:

import csv, sys rows = list(csv.reader(sys.stdin)) writer = csv.writer(sys.stdout) for j in xrange(0, len(rows)):   writer.writerow([row[j] for row in rows:  print row[j]rows]) 

CSV parsing is not easily done with POSIX tools only, unless you are using a simplified CSV variant with no quoting (so that commas can't appear in a field). Even then this task doesn't seem easy to do with awk or other text processing to tool. You can use Perl with Text::CSV, Python with csv, R with read.csv, Ruby with CSV, … (All of these are part of the standard library of the respective language except for Perl.)

For example, in Python:

import csv, sys rows = csv.reader(sys.stdin) for j in xrange(0, len(rows)): for row in rows:  print row[j] 

CSV parsing is not easily done with POSIX tools only, unless you are using a simplified CSV variant with no quoting (so that commas can't appear in a field). Even then this task doesn't seem easy to do with awk or other text processing to tool. You can use Perl with Text::CSV, Python with csv, R with read.csv, Ruby with CSV, … (All of these are part of the standard library of the respective language except for Perl.)

For example, in Python:

import csv, sys rows = list(csv.reader(sys.stdin)) writer = csv.writer(sys.stdout) for j in xrange(0, len(rows)):   writer.writerow([row[j] for row in rows]) 
Source Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k
Loading