1

My questions is the same as Command line CSV viewer? and the column -s, -t command answer is almost perfect - except it doesn't seem to handle empty csv fields as I'd expect. eg given input

col1,,col3
,col2,

produces:

col1  col3
col2

but I would like:

col1      col3
     col2

Is there an option to achieve the above with column command or an alternative way to achieve this? (cygwin environment)

Community
  • 1
  • 1
hanlonj
  • 319
  • 4
  • 16

4 Answers4

2

-n option exists in debian. e.g.:

>$ echo -e "col1,,col3\n,col2," | column -n -s, -t
col1        col3
      col2  
user1707094
  • 89
  • 1
  • 2
2

It's the furthest thing from elegant, and it's probably something you've already thought of and are looking for a better solution, but I work around this annoyance by doing a series of sed replacements to put a whitespace in empty fields. I have these as functions in my bashrc...

csvcolumn() { sed -e "s/^$2/ $2/" -e "s/$2$/$2 /" -e "s/$2$2/$2 $2/g"  -e "s/$2$2/$2 $2/g" $1 | column -t -s$2 ; }
csvcomma() { sed -e 's/^,/ ,/' -e 's/,$/, /' -e 's/,,/, ,/g'  -e 's/,,/, ,/g' $1 | column -t -s, ; }

The first one takes two args to be able to specify the delimiter character. The second is the same thing but it only takes one arg and assumes the delimiter is a comma since that's most often what I use anyway.

csvcolumn input.csv ,

or

csvcomma input.csv
Costa
  • 2,043
  • 1
  • 14
  • 27
1

Use cat and sed.

cat filename | sed 's/[~\],/\t/g'
Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83
0

If you're a vimmer, use the CSV plugin, which is juuust beautiful.

Myer
  • 3,670
  • 2
  • 39
  • 51