Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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
share|improve this answer
    
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
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.