Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script to properly align columns

Tags:

bash

shell

I have a (3-columns) TAB-delimited file, such as:

activity_log    manager Manager
reserve_rm_hreserver_rm_log manager Manager
mo  apprv_mgr1  Approving Manager
wrview  manager Manager

I'd like to get columns correctly aligned; in this case:

activity_log                  manager      Manager
reserve_rm_hreserver_rm_log   manager      Manager
mo                            apprv_mgr1   Approving Manager
wrview                        manager      Manager

No doubt this is doable with awk, by scanning the longest string in every column, and using that for formatting the column printing. I could do that.

But I'm sure there must be a one-liner to do that much more easily. Am I right?

like image 338
user3341592 Avatar asked Aug 08 '26 08:08

user3341592


1 Answers

This should work:

$ column -t myfile.txt

you can use different chars to fill (man column is your friend)

like image 131
mauro Avatar answered Aug 11 '26 05:08

mauro