gpt4 book ai didi

shell - UNIX : Creating a table formatted ouput from a delimited text file

转载 作者:行者123 更新时间:2023-12-04 14:35:31 25 4
gpt4 key购买 nike

我需要从文本文件中获取表格格式输出,我正在通过以下 awk 命令实现它。

分隔文件

ACTIVE#1238917238971238#USA#The U.S. is a country of 50 states covering a vast swath of North America.
ACTIVE#21389721839781237812#INDIA#India, officially the Republic of India, is a country in South Asia.
ACTIVE#3121278372183782137812#AUSTRALIA#Australia, officially the Commonwealth of Australia, is a sovereign country comprising the mainland of the Australian continent

AWK 命令
awk -F"#" 'BEGIN {{printf "%-80s\n","--------------------------------------------------------------------------------------------------------------------------------------"} {printf "|%-12s|%-30s|%-38s|%-50s|\n","STATUS","ID", "Country", "Description"} {printf "%-80s\n","--------------------------------------------------------------------------------------------------------------------------------------"}} {printf "|%-12s|%-30s|%-38s|%-50s|\n",$1,$2,$3,$4} END{printf "%-80s\n","--------------------------------------------------------------------------------------------------------------------------------------"}' /tmp/test.txt

输出:
enter image description here

如果您可以看到 Description 列的输出,则它不会在其自己的列中格式化输出,而是由于字符串长度而弄乱了整个表。

有人可以查看并建议我如何更好地显示说明列的输出吗?

最佳答案

我会用 perl 来代替,使用 Term::Table模块(可通过操作系统的包管理器安装或从 CPAN 安装),它将根据需要自动计算列宽和换行:

#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/say/;
use Term::Table;

my @lines = map { chomp; [ split /#/ ] } <>;
say for Term::Table->new(
max_width => 80,
header => ["Status", "ID", "Country", "Description"],
rows => \@lines
)->render;

用法示例:

$ ./table.pl < input.txt
+--------+--------------------------+-----------+--------------------------+
| Status | ID | Country | Description |
+--------+--------------------------+-----------+--------------------------+
| ACTIVE | 1238917238971238 | USA | The U.S. is a country of |
| | | | 50 states covering a va |
| | | | st swath of North Americ |
| | | | a. |
| | | | |
| ACTIVE | 21389721839781237812 | INDIA | India, officially the Re |
| | | | public of India, is a co |
| | | | untry in South Asia. |
| | | | |
| ACTIVE | 3121278372183782137812 | AUSTRALIA | Australia, officially th |
| | | | e Commonwealth of Austra |
| | | | lia, is a sovereign coun |
| | | | try comprising the mainl |
| | | | and of the Australian co |
| | | | ntinent |
+--------+--------------------------+-----------+--------------------------+

想想看,它也可以在没有任何非核心模块的情况下完成,感谢 perl formats .我实际上更喜欢这种方式,因为它可以更好地自动换行(尽管更改表格的整体宽度甚至单个列的宽度会变得更加麻烦):

#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/say/;

my ($status, $id, $country, $description);
while (<>) {
chomp;
($status, $id, $country, $description) = split /#/;
write;
}
say "+--------+------------------------+-----------+-------------------------------+";

format STDOUT_TOP =
+--------+------------------------+-----------+-------------------------------+
| Status | Id | Country | Description |
+--------+------------------------+-----------+-------------------------------+
.

format STDOUT =
| @<<<<< | @<<<<<<<<<<<<<<<<<<<<< | @<<<<<<<< | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
$status, $id, $country, $description
|~~ | | | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
$description
| | | | |
.

$ ./table.pl < input.txt
+--------+------------------------+-----------+-------------------------------+
| Status | Id | Country | Description |
+--------+------------------------+-----------+-------------------------------+
| ACTIVE | 1238917238971238 | USA | The U.S. is a country of 50 |
| | | | states covering a vast swath |
| | | | of North America. |
| | | | |
| ACTIVE | 21389721839781237812 | INDIA | India, officially the |
| | | | Republic of India, is a |
| | | | country in South Asia. |
| | | | |
| ACTIVE | 3121278372183782137812 | AUSTRALIA | Australia, officially the |
| | | | Commonwealth of Australia, is |
| | | | a sovereign country |
| | | | comprising the mainland of |
| | | | the Australian continent |
| | | | |
+--------+------------------------+-----------+-------------------------------+

关于shell - UNIX : Creating a table formatted ouput from a delimited text file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61628505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com