-1

Consider this usage of column(1):

column -s, -t <<CSV
a,b,c
,ee,ff
CSV

The above snippet emits:

a   b   c
ee  ff

But I was expecting to see something like this:

a   b   c
    ee  ff

column seems to be dropping the leading blank "cell" from the 2nd row, while I would expect and want it to be preserved.

Is there any way to disable this blank-stripping behavior in column? Are there any standard/recommended workaround suggestions?

I tried using rs(1), but I had other problems with that command: rs(1) prints all output in a single line; how to duplicate the input array shape?

shadowtalker
  • 1,328

4 Answers4

1

The column utility removes empty columns from the start and end of each line. This gives rise to the misaligned columns that you observe. There are no options for the utility to disable this behaviour.

Would you want to convert a general CSV document to another format, such as TSV, a safer way would be to use a CSV-aware tool such as Miller:

$ mlr --icsv --otsv cat <<END_DATA
a,b,c,"d,e"
,ee,ff,"ok ""hello"""
END_DATA
a       b       c       d,e
        ee      ff      "ok ""hello"""
$ mlr --icsv --opprint cat <<END_DATA
a,b,c,"d,e"
,ee,ff,"ok ""hello"""
END_DATA
a b  c  d,e
- ee ff ok "hello"
$ mlr --icsv --opprint --barred cat <<END_DATA
a,b,c,"d,e"
,ee,ff,"ok ""hello"""
END_DATA
+---+----+----+------------+
| a | b  | c  | d,e        |
+---+----+----+------------+
| - | ee | ff | ok "hello" |
+---+----+----+------------+
Kusalananda
  • 333,661
1

Using Raku (formerly known as Perl_6)

You can use Raku's Text::CSV module: it will output RFC-4180 (and possibly RFC-4180/RFC-7111) conforming files. Needless to say, blank leading cells are retained. Below, output with a different column separator (tab) demonstrating:

~$ raku -MText::CSV -e 'my @a = csv(in => $*IN); csv(in => @a, out => $*OUT, sep => "\t");'  file

Sample Input:

a,b,c
,ee,ff

Sample Output (tab separated):

a   b   c
    ee  ff

See the link below if your file is huge and/or you want to read your CSV files linewise.

https://unix.stackexchange.com/a/722776/227738 https://github.com/Tux/CSV/blob/master/doc/Text-CSV.pdf
https://raku.org

jubilatious1
  • 3,195
  • 8
  • 17
0

If "non-standard" tools are OK, the XSV toolkit can nicely format more or less arbitrary delimited-text data:

xsv table <<CSV
a,b,c
,ee,ff
CSV
a   b   c
    ee  ff
shadowtalker
  • 1,328
0

If your data is "simple CSV", with no quoted commas or newlines, so that a comma can only occur to separate fields, you could use awk to get the effect you want:

awk -F, 'BEGIN {OFS="\t"} {$1=$1; print}' <<CSV
a,b,c
,ee,ff
CSV

Output

a       b       c
        ee      ff
Chris Davies
  • 116,213
  • 16
  • 160
  • 287