bash:保存带有与列对齐的空间的选项卡

发布于 2025-02-12 18:50:22 字数 945 浏览 2 评论 0原文

我正在尝试显示.tsv文件很好地对齐为列,但允许将显示到当前屏幕宽度。我能够以以下方式完成此操作,但如果输入包含使用的特定字符,则将失败。我目前使用的当前解决方案如下:

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

我尝试直接使用标签本身,但无法使其正常工作。对于的默认选项,使用任何whitespace,而不仅仅是选项卡,因此它对我不起作用。比上述更好的选择要感谢任何更好的选择。

PS: 样本如下

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:~$

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:~$

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美煞众生 2025-02-19 18:50:22

您可以将列设置为将选项卡用作字符,用于用-s划定列:

列-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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文