正在查找awk格式语法
如何在awk中格式化输出?或ksh打印命令 我有一份档案正在查找awk格式语法,awk,Awk,如何在awk中格式化输出?或ksh打印命令 我有一份档案 bash-3.2# cat filename host 1 host1 2 1 host2 3 2 1 host3 1 2 host4 3 2 1 希望像下面这样对齐输出 host1 2 1 host2 3 2 1 host3 2 1 host4 3
bash-3.2# cat filename
host 1
host1 2 1
host2 3 2 1
host3 1 2
host4 3 2 1
希望像下面这样对齐输出
host1 2 1
host2 3 2 1
host3 2 1
host4 3 2 1
host5 1
假设示例数据中有输入错误(主机1实际上是主机5 1)。您可以使用GNU awk执行类似的操作: script.awk的内容:
我真的看不到模式…模式是在没有值的情况下不打印任何内容,并将值按降序排列。@doubleDown按降序排列自己列中的所有数字?为什么“主机”转换为“主机5”?主机1中的
2
从何而来?
NR==FNR {
for (c = 2 ; c <= NF ; c++) {
high = (high > $c) ? high : $c
}
next
}
{
delete a
delete b
n = p = shift = 0
printf "%s\t",$1
for (i = 2 ; i <= NF ; i++) {
a[$i]
}
n = asorti(a,b)
for (x = n ; x > 0 ; x--) {
if (b[x] < high) { shift = high - b[x] }
while (++p <= shift) {
printf "\t"
}
printf "%s\t", b[x]
}
print ""
}
$ cat file
host1 2 1
host2 3 2 1
host3 1 2
host4 3 2 1
host5 1
$ gawk -f script.awk file file
host1 2 1
host2 3 2 1
host3 2 1
host4 3 2 1
host5 1
$ cat file2
host1 2 1
host2 3 2 1
host3 1 2
host4 3 5 1
host5 1
$ gawk -f script.awk file2 file2
host1 2 1
host2 3 2 1
host3 2 1
host4 5 3 1
host5 1