14

I have file with columns spearated with tab.

I have file when some rows have empty cells (on begining, in middle).

In such cases column -t -s $'\t' simply fails:

Input:

$ echo -e 'A\tB\tC\tD\n\tb1\t\td1\n\t\t\td2\na3\t\t\td3' > in.tsv
$ hexdump -C in.tsv 
00000000  41 09 42 09 43 09 44 0a  09 62 31 09 09 64 31 0a  |A.B.C.D..b1..d1.|
00000010  09 09 09 64 32 0a 61 33  09 09 09 64 33 0a        |...d2.a3...d3.|
0000001e

column output:

$ cat in.tsv | column -t -s $'\t'
A   B   C  D
b1  d1
d2
a3  d3

instead of:

A       B       C       D
        b1              d1
                        d2
a3                      d3

Could you recommend how to do TSV command line formatting ? (in Unix way, I want to pipe output of program into formatter, like column)

Any way of "fixing" column approach? Maybe another tool?

2 Answers2

13

You can just use Debian's column. It provides the option -n which makes it work exactly how you want.

Alternatively, you can put a space in the empty columns, using sed:

sed ':x s/\(^\|\t\)\t/\1 \t/; t x' < in.tsv | column -t -s $'\t'

example:

$ sed ':x s/\(^\|\t\)\t/\1 \t/; t x' < in.tsv | column -t -s $'\t'
A   B   C  D
    b1     d1
           d2
a3         d3
angus
  • 12,321
  • 3
  • 45
  • 40
  • I am afraid how sed will behave with alternative '^|\t'... due to '^' is not specifying atoms. (So, is it going to substitute \1 with empty string? – Grzegorz Wierzowiecki Jan 13 '12 at 14:22
  • Yes, \(^\) alone matches an empty string, anchored to the beginning of line. \1 "produces a copy" of that empty string. – angus Jan 13 '12 at 16:22
0
sed 's/||/| |/g;s/||/| |/g' filename-here

The above command is for pipe so replace it with tabspace.

You just need to replace empty columns with a blank space and pipe the output to the command you are already using.

Rohit
  • 111