小编Man*_*mar的帖子

UNIX:从带分隔符的文本文件创建表格格式的输出

我需要从文本文件中获取表格格式输出,我正在通过以下 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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

输出: 在此处输入图片说明

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

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

unix shell awk

3
推荐指数
1
解决办法
93
查看次数

标签 统计

awk ×1

shell ×1

unix ×1