首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >awk:模仿'pr -mt文件*‘

awk:模仿'pr -mt文件*‘
EN

Unix & Linux用户
提问于 2017-08-03 12:25:22
回答 1查看 177关注 0票数 1

当我在玩awk的S多文件处理构造时

代码语言:javascript
运行
复制
awk 'NR == FNR { # some actions; next} # other condition {# other actions}' file*.txt

我问自己,是否可以将不同大小的文本文件转换为使用awk打印的文本文件,比如

代码语言:javascript
运行
复制
pr -mt file*

假设:

file1.txt

代码语言:javascript
运行
复制
arbitrary text of the first file,
which is not so long.

More arbitrary text of the first file.

file2.txt:

代码语言:javascript
运行
复制
Arbitrary text of the second file.
More arbitrary text of the second file.
More and More arbitrary text of the second file.
It's going on.
But finally every text will end.

产出应该是这样的:

代码语言:javascript
运行
复制
$ pr -w150 -mt file*
arbitrary text of the first file,         Arbitrary text of the second file.            
which is not so long.                     More arbitrary text of the second file.       
                                          More and More arbitrary text of the second file.  
More arbitrary text of the first file.    It's going on.                    
                                          But finally every text will end.  

如何使用awk命令来实现这一点,只在file*.txt上使用?

EN

回答 1

Unix & Linux用户

回答已采纳

发布于 2017-08-03 13:04:36

您可以记录每个文件的所有行,记录每个文件的最大行长和行数,然后打印所有行:

代码语言:javascript
运行
复制
awk '
  FNR == 1 {f++}
  {line[f, FNR] = $0}
  length > width[f] {width[f] = length}
  FNR > max_lines {max_lines = FNR}
  END{
    for (row = 1; row <= max_lines; row++) {
      for (i = 1; i <= f; i++)
        printf "%-*s", (i == f ? 0 : width[i] + 2), line[i, row]
      print ""
    }
  }' ./*.txt
票数 4
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/383677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档