BASH 中的 nawk 和 OFS 问题
我正在编写一个脚本来提取处理器集编号,后跟 bash shell 中 Solaris 中该处理器集下的处理器 ID:
这是我想要从中提取的输出:($output 的内容)
user processor set 1: processors 0 1
user processor set 2: processors 2 8 9
user processor set 3: processors 3 4 5 6 7
是:
1: 0 1
2: 2 8 9
3: 3 4 5 6 7
所需的输出 我使用 nawk 编写的代码:
print $output | nawk '
BEGIN { ORS="\n" ; OFS = " " }
{
print$4; print OFS
for (i=6;i<=NF;i++)
print $i
}'
获得的输出:
1:
0
1
2:
2
8
9
3:
3
4
5
6
7
任何人都可以帮忙并让我知道在获得所需的输出时我缺少什么。 提前致谢。
编辑:使用 OFS 和 ORS 的想法可以从本教程中获得: 教程链接
I am working on a script to extract the processor set number followed by the processor ids that fall under that processor set in Solaris in bash shell:
Here is the output I want to extract from: (contents of $output)
user processor set 1: processors 0 1
user processor set 2: processors 2 8 9
user processor set 3: processors 3 4 5 6 7
Desired output is:
1: 0 1
2: 2 8 9
3: 3 4 5 6 7
The code i wrote using nawk:
print $output | nawk '
BEGIN { ORS="\n" ; OFS = " " }
{
print$4; print OFS
for (i=6;i<=NF;i++)
print $i
}'
obtained output:
1:
0
1
2:
2
8
9
3:
3
4
5
6
7
Can anyone help and let me know what I am missing from obtaining the desired output .
Thanks in advance.
EDIT: Idea to use OFS and ORS are obtained from this tutorial: tutorial link
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,
ORS
已设置为"\n"
。由于您想使用多个打印语句,因此您需要将其设置为空字符串,因为任何打印语句后面都有一个隐式的print ORS
。你也可以用 cut 来做到这一点:
ORS
is already set to"\n"
by default. Since you want to use multiple print statements you'll want to set it to the empty string since there's an implicitprint ORS
after any print statement.You could also do this with cut:
试试这个
Try this