当我在玩awk的S多文件处理构造时
awk 'NR == FNR { # some actions; next} # other condition {# other actions}' file*.txt我问自己,是否可以将不同大小的文本文件转换为使用awk打印的文本文件,比如
pr -mt file*假设:
file1.txt
arbitrary text of the first file,
which is not so long.
More arbitrary text of the first file.file2.txt:
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.产出应该是这样的:
$ 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上使用?
发布于 2017-08-03 13:04:36
您可以记录每个文件的所有行,记录每个文件的最大行长和行数,然后打印所有行:
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 ""
}
}' ./*.txthttps://unix.stackexchange.com/questions/383677
复制相似问题