开发者

How to line up columns using paste(1)? or how to make an aligned table merging lines in the shell?

I want to merge lines such that the merged lines are aligned on the same boundary. UNIX paste(1) does this nicely when lines all meet at the same tab boundary, but when lines differ in size (in the file that lines are being merged into), the text comes out awkward.

Example of paste(1) that has the desired effect:

$ echo -e "a\nb\nccc\nd" | paste - -
a       b
ccc     d

Example of paste(1) with undesired effect:

$ echo -e "a\nb\ncccccccccccc\nd开发者_开发技巧" | paste - -
a       b
cccccccccccc    d

Note how the 2nd column doesn't line up. I want 'b' to line up with 'd', which requires an additional tab. Unfortunately I believe this is the limit for the paste utility, so if anyone has any idea of how to get the desired effect above, I'd love to hear it.


Check out the column utility...

$ echo -e "a\nb\ncccccccccccc\nd" | paste - - | column -t
a             b
cccccccccccc  d


See the answer here for a easy way to handle this when the lines may have spaces or other characters already using the pr command. For example:

pr -m -t -w 200 file1 file2


You can use the printf utility to create formatted output, using the same format specifiers as printf. With the format specifiers, you can specify a minimum field width. For example:

printf "%30s%30s\n" "Alpha" "Bravo"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜