awk 在固定列中打印数据

awk 在固定列中打印数据

我想打印固定列中的数据。我的意思是列应该像表格中一样。我使用了 printf。输入:

To find information use the search box on the top right corner of the screen, or categorically browse the Wiki using the Documentation topic links provided below.

Find something you would like to add or edit? The Getting started section (below) gives contributors a few pointers on how to start editing articles.

Both official documentation as well as community-contributed contents can be found on the Wiki. Official documents (created by the Gentoo Documentation Team) are located in the Gentoo Project name space. 

AWK:

{
for(i=1;i<=5;i++){
printf "%20s",$i
if (i==5){print "\n"}}
}

结果

          To                find         information                 use                 the



        Find           something                 you               would                like



        Both            official       documentation                  as                well

列的左侧和下方有大量空间。还有其他方法可以显示吗?

答案1

awk -v numlines=$( wc -l t | awk '{print $1}' ) '{ if (NF>0) { for (i=1;i<=5;i++) printf("%-20s ",$i) ; if (NR != numlines) { print "" } } }' inputfile

'numlines=$(' 部分将输入文件中的行数放入 awk 可用的名为 numlines 的变量中。'NF>0' 确保忽略空行。'printf(%-20s' 确保字段左对齐且宽度为 20 个字符。'NR != numlines' 在文件末尾以外的地方打印换行符。

相关内容