Search code examples
bashshellcuttext-formatting

How to write output of a bash command in tabular format with a pre-defined spacing


 2   0   0   184
 2   0   0   184
 765311      61423       6454323302  470948
 598500      678403      6454288800  1810469

I used grep <command output> | cut -d ':' -f 3 | column -x >> $temp to write data in tabular format. But there seem to be huge difference in length of strings which causes the table to skew. I need to format these with equal spacing. How can I do that?

Edit: The file that the command runs on contains text as following:

total_STDIO_OPENS: 176400
total_STDIO_FDOPENS: 0
total_STDIO_READS: 5881999200
total_STDIO_WRITES: 276435
total_STDIO_SEEKS: 0
total_STDIO_FLUSHES: 0
total_STDIO_BYTES_WRITTEN: 7689144
total_STDIO_BYTES_READ: 53954527200
total_STDIO_MAX_BYTE_READ: 917480
total_STDIO_MAX_BYTE_WRITTEN: 176
total_STDIO_FASTEST_RANK: 81759
total_STDIO_FASTEST_RANK_BYTES: 917712
total_STDIO_SLOWEST_RANK: 137230
total_STDIO_SLOWEST_RANK_BYTES: 917729
total_STDIO_F_META_TIME: 235319.135093
total_STDIO_F_WRITE_TIME: 2.271446
total_STDIO_F_READ_TIME: 1204.541221
total_STDIO_F_OPEN_START_TIMESTAMP: 0.000000
total_STDIO_F_CLOSE_START_TIMESTAMP: 0.000000
total_STDIO_F_WRITE_START_TIMESTAMP: 6.714122
total_STDIO_F_READ_START_TIMESTAMP: 0.000000
total_STDIO_F_OPEN_END_TIMESTAMP: 6.782347
total_STDIO_F_CLOSE_END_TIMESTAMP: 6.856372
total_STDIO_F_WRITE_END_TIMESTAMP: 346.306913
total_STDIO_F_READ_END_TIMESTAMP: 6.856351
total_STDIO_F_FASTEST_RANK_TIME: 0.000000
total_STDIO_F_SLOWEST_RANK_TIME: 0.000000
total_STDIO_F_VARIANCE_RANK_TIME: 0.000000
total_STDIO_F_VARIANCE_RANK_BYTES: 0.000000

I am grep'ing using grep -Ein 'total_stdio_read|total_posix_write|total_stdio_write|total_posix_read' $file | cut -d ':' -f 3 | column -x >> $temp

Desired output is:

2           0           0           184
2           0           0           184
765311      61423       6454323302  470948
598500      678403      6454288800  1810469

Solution

  • So what worked for me was mix of solutions.

    <command output> | cut -d ' ' -f 2 | column -x | xargs -n4 printf '%-10d %-10d %-10d %-10d\n'