How can I do the following to a CSV file using sed or awk?
- Delete a column
- Duplicate a column
- Move a column
I have a big table with over 200 rows, and I'm not that familiar with sed.
|
|
Aside from how to cut and re-arrange the fields (covered in the other answers), there is the issue of quirky CSV fields. If your data falls into this "quirky" category, a bit of pre and post filtering can take care of it. The filters shown below require the characters Here are the filters wrapped around a simple Note: field-five has an invalid/incomplete "quoted field" layout, but it is benign at the end of a row (depending on the CSV parser). But, of course, it would cause problematic unexpedted results if it were to be swapped away from its current end-of-row position. Update; user121196 has pointed out a bug when a comma precedes a trailing quote. Here is the fix. The data
The code
The output:
Here is the pre filter, expanded with comments.
|
|||||||||
|
|
This depends on whether your CSV file uses commas only for delimiters, or if you have madness like:
This assumes you're using a simple CSV file: Removing a columnYou can get rid of a single column many ways; I used column 2 as an example. The easiest way is probably to use
If you actually need to use
There are a number of ways to do this in
I find it easier to output field 1 and then use
This is annoying for columns further along though Duplicating a columnIn
In
The
(tcdyl came up with a better method in his answer) Moving a columnI think the |
||||
|
Given a space-delimited file in the following format:
You can remove field 2 with awk like so:
which returns
Replace column 2 with column n where appropriate. To duplicate column 2,
which returns
To switch column 2 and 3,
which returns
awk is generally very good at dealing with the concept of fields. If you're dealing with a CSV, and not a space-delimited file, you can simply use
to define your field as a comma, instead of a space (which is the default). There are a number of good awk resources online, one of which I list as a source below. Source for #3 |
|||||||
|
|
To remove a column, not print it:
To change the order:
Re-direct to an output file.
|
|||||||
|