bash脚本产生以下输出:
Computer_1 Netcom 1 Status_ok
Computer_1 Netcom 2 Status_ok
Computer_1 Netcom 3 Status_ok
Computer_1 Netcom 4 Status_ok
Computer_1 Netcom 5 Status_ok
Computer_1 Netcom 6 Status_ok
Computer_1 Netcom 7 Status_ok
Computer_1 Netcom 8 Status_ok
Computer_2 Netcom 9 Status_ok
Computer_2 Netcom 10 Status_ok
Computer_2 Netcom 11 Status_ok
Computer_2 Netcom 12 Status_ok
Computer_2 Netcom 13 Status_ok
Computer_2 Netcom 14 Status_ok
Computer_2 Netcom 15 Status_ok
Computer_2 Netcom 16 Status_ok
Computer_3 Netcom 17 Status_ok
Computer_3 Netcom 18 Status_ok
Computer_3 Netcom 19 Status_ok
Computer_3 Netcom 20 Status_ok
Computer_3 Netcom 21 Status_ok
Computer_3 Netcom 22 Status_ok
Computer_3 Netcom 23 Status_ok
Computer_3 Netcom 24 Status_ok
Computer_4 Netcom 25 Status_ok
Computer_4 Netcom 26 Status_ok
Computer_4 Netcom 27 Status_ok
Computer_4 Netcom 28 Status_ok
Computer_4 Netcom 29 Status_ok
Computer_4 Netcom 30 Status_ok
Computer_4 Netcom 31 Status_ok
Computer_4 Netcom 32 Status_ok
但是,我需要将输出组批量格式化为四列:
Computer_1 Computer_2 Computer_3 Computer_4
Netcom 1 Status_ok Netcom 9 Status_ok Netcom 17 Status_ok Netcom 25 Status_ok
Netcom 2 Status_ok Netcom 10 Status_ok Netcom 18 Status_ok Netcom 26 Status_ok
Netcom 3 Status_ok Netcom 11 Status_ok Netcom 19 Status_ok Netcom 27 Status_ok
Netcom 4 Status_ok Netcom 12 Status_ok Netcom 20 Status_ok Netcom 28 Status_ok
Netcom 5 Status_ok Netcom 13 Status_ok Netcom 21 Status_ok Netcom 29 Status_ok
Netcom 6 Status_ok Netcom 14 Status_ok Netcom 22 Status_ok Netcom 30 Status_ok
Netcom 7 Status_ok Netcom 15 Status_ok Netcom 23 Status_ok Netcom 31 Status_ok
Netcom 8 Status_ok Netcom 16 Status_ok Netcom 24 Status_ok Netcom 32 Status_ok
实现以下输出的高性能解决方案是什么?
答案 0 :(得分:2)
pr
可以执行按列格式化。我在这里使用awk
来拆分列标题:
script.sh |
awk '
$1 != prev {print $1; prev=$1}
{for (i=2; i<=NF; i++) {$(i-1)=$i}; NF--; print}
' |
pr -t4 -w84 |
expand
输出
Computer_1 Computer_2 Computer_3 Computer_4
Netcom 1 Status_ok Netcom 9 Status_ok Netcom 17 Status_ok Netcom 25 Status_ok
Netcom 2 Status_ok Netcom 10 Status_ok Netcom 18 Status_ok Netcom 26 Status_ok
Netcom 3 Status_ok Netcom 11 Status_ok Netcom 19 Status_ok Netcom 27 Status_ok
Netcom 4 Status_ok Netcom 12 Status_ok Netcom 20 Status_ok Netcom 28 Status_ok
Netcom 5 Status_ok Netcom 13 Status_ok Netcom 21 Status_ok Netcom 29 Status_ok
Netcom 6 Status_ok Netcom 14 Status_ok Netcom 22 Status_ok Netcom 30 Status_ok
Netcom 7 Status_ok Netcom 15 Status_ok Netcom 23 Status_ok Netcom 31 Status_ok
Netcom 8 Status_ok Netcom 16 Status_ok Netcom 24 Status_ok Netcom 32 Status_ok
答案 1 :(得分:0)
这可能是一个更好的lottttt,但单向
其中$ 1是bash脚本的输出。
我使用awk删除第一个字段。 然后迭代行数并从输出中读取每第8行。 然后将换行符翻译成标签,使其看起来很整洁。
显然,如果您有不断变化的记录或行数,或者您可以在脚本开头设置它们并替换我的硬编码数字。
#!/bin/bash
echo -e "Computer_1\t\tComputer_2\t\tComputer_3\t\tComputer_4"
line=$(awk '{ print $2, $3, $4 }' $1)
for ((i = 1 ; i < 9 ; i++)); do
linez=$(sed -n $i'~8p' <<< "$line")
linez2=$(tr "\n" "\t" <<< "$linez")
echo "$linez2"
done