0
 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
6
  • 2
    Please add sample input (no descriptions, no images, no links) and your desired output for that sample input to your question (no comment). Commented Apr 13, 2020 at 16:20
  • I am getting the output but I don't know how to format it. Commented Apr 13, 2020 at 16:30
  • Your sample input file only matches two lines with that grep pattern... something that produces results that match the numbers in your desired output would be useful. Commented Apr 13, 2020 at 17:47
  • It's a similar file just different numbers. Commented Apr 13, 2020 at 17:51
  • 1
    column -t might help. Commented Apr 13, 2020 at 18:31

3 Answers 3

3

I'm assuming you want a length of 10 characters per column plus 2 characters extra spacing. Increase the length if the data doesn't fit into the columns (data is not truncated).

xargs and printf:

$ <your commands> | xargs -n4 printf '%-10s  %-10s  %-10s  %-10s\n'
2           0           0           184
2           0           0           184
765311      61423       6454323302  470948
598500      678403      6454288800  1810469

awk and printf:

$ <your commands> | awk '{for(i=1;i<=NF;i++) printf "%-10s%s", $i, (i==NF ? ORS : "  ")}'
2           0           0           184
2           0           0           184
765311      61423       6454323302  470948
598500      678403      6454288800  1810469

Or simply column -t:

$ <your commands> | column -t
2       0       0           184
2       0       0           184
765311  61423   6454323302  470948
598500  678403  6454288800  1810469
Sign up to request clarification or add additional context in comments.

4 Comments

%-10s doesn't print anything to the terminal for some reason so I tried replacing it with %-10d but that result in expected a numeric value error
You need one argument for each %... format option, e.g. in the first example there are four arguments on each line (-n4) and four %-10s format options and in the second example there are two of them (the actual value and spacing/newline).
by %-10s what I meant was %-10s %-10s %-10s %-10s
I love column -t. Great idea.
2

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'

Comments

1

Use the BASH 'printf' builtin. This has the same syntax for formatting as C printf() so there are many options.

Example:

$ printf "%-20s %3f\n" abc 3.4
abc                  3.400000

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.