gpt4 book ai didi

awk - 如何并排打印输出?

转载 作者:行者123 更新时间:2023-12-02 09:03:34 24 4
gpt4 key购买 nike

我有以下脚本输出:

192.168.0.10
192.168.0.10
undefined
192.168.0.130
192.168.0.130
ansible
192.168.0.141
192.168.0.141
undefined
192.168.0.252
192.168.0.252
undefined

但我希望它是这样的:

192.168.0.10 192.168.0.10 undefined
192.168.0.130 192.168.0.130 ansible
192.168.0.141 192.168.0.141 undefined
192.168.0.252 192.168.0.252 undefined

我已经看到如何让 2 条线彼此相邻,但我在 3 处失败了 ^^

awk '{getline b;printf("%s %s\n",$0,b)}'

最佳答案

使用awk,请尝试以下操作。

awk '{count++;printf("%s%s",$0,count%3==0?ORS:OFS)} END{if(count%3!=0){print ""}}' Input_file

说明:为上述代码添加详细说明。

awk '                                       ##Starting awk program from here.
{
count++ ##Increasing variable count value with 1 here.
printf("%s%s",$0,count%3==0?ORS:OFS) ##Printing current line and either new line(ORS) or space(OFS) based opon condition if count is divided by 3 or NOT.
}
END{ ##Starting END block for this awk code here.
if(count%3!=0){ ##Checking condition if count is NOT fully divided by 3 then do following.
print "" ##Printing null variable here in case previous line was not having new line so to add it in last.
}
}
' Input_file ##Mentioning Input_file name here.

关于awk - 如何并排打印输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61073338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com