Home > other >  bash: conserve tab with spaces for alignment with column
bash: conserve tab with spaces for alignment with column

Time:07-05

I am trying to display .tsv files aligned nicely as columns, and yet allow limiting display to the current screen width. I am able to get this done in the following way that works in general but will fail if the input contains a particular character that is used by column. The current solution that I am using presently works as follows:

bash$ cat sample.tsv | tr '\t' '@' | column -n -t -s @ | cut -c-`tput cols`

I tried using tab itself directly but could not make it work. And with default option for column, any whitespace and not just tabs are used so it does not work for me. Would be thankful for any better alternative than the above.

PS: A sample is shown below

bash:~$ cat sample.tsv
Sl      Name    Number  Status
1       W Jhon   1 234 4454     y
2       M Walter         2 232 453      n
3       S M Ray  1 343 453      y
bash:~$ cat sample.tsv | tr '\t' '@' | column -n -t -s @ | cut -c-`tput cols`
Sl  Name      Number       Status
1   W Jhon     1 234 4454  y
2   M Walter   2 232 453   n
3   S M Ray    1 343 453   y
bash:~$ cat sample.tsv | column -n -t | cut -c-`tput cols`
Sl  Name  Number  Status
1   W     Jhon     1      234  4454  y
2   M     Walter   2      232  453   n
3   S     M       Ray      1   343   453  y
bash:~$

CodePudding user response:

You can set column to use tab as character to be used to delimit columns with -s:

column -t -s $'\t' -n sample.tsv

Sl  Name      Number       Status
1   W Jhon     1 234 4454  y
2   M Walter   2 232 453   n
3   S M Ray    1 343 453   y
  • Related