Using sed
or awk
, is it possible to visually align columns in a CSV file?
For example:
e.g. from:
a,b,c,some stuff,"some, other, stuff",d,2023-03-10 18:37:00
y,x,z,t,cool,thing,2022-04-12 21:44:00
to:
a, b, c, some stuff,"some, other, stuff", d, 2023-03-10 18:37:00<EOL>
x, y, z, t, cool, thing, 2022-04-12 21:44:00<EOL>
There are some double quoted fields containing text and having commas.
I gave column
from bsdmainutils
a try but it's apparently not able to deal with such data.
Best Answer
A CSV file of this type:
is not really the same data file anymore since you are modifying the fields. When parsed, what was originally
"t"
will now parse out as" t"
because of the width of"some stuff"
above (unless you use a regex to parse the non-standard,[variable space]
delimiters.)You can force quotes on all fields to get a more csv file that shows these new fields clearly. Here is a Ruby to do that:
Prints:
Or, if you really want quoted and unquoted fields, you can do:
Prints:
Which also handles nasty escaped quotes within fields. Given:
The second version prints: