I have several huge CSV files in which I want to swap two column names.
I do not want to modify/copy/rewrite the data.
The operation is very cheap in C
: fopen
the file, fgets
the header, fseek
or rewind
, manipulate the header (preserving its length), fputs
the new header, fclose
the file.
This can also be done in ANSI Common Lisp
(CLISP, SBCL or GCL):
(with-open-file (csv "foo.csv" :direction :io
:if-exists :overwrite)
(let ((header (read-line csv)))
(print header)
(file-position csv 0)
(write-line (string-upcase header) csv)
(file-position csv 0)
(read-line csv)))
and takes a fraction of a second (sed
takes a few minutes because it reads and re-writes the whole file even it you tell it to modify just the first line, ignoring the crucial information that the size of the header did not change).
How do I do that with the "standard unix tools" (e.g., perl
)?