2011-05-06 76 views
1

我上的脚本工作,提取处理器集数后面是该处理器的Solaris在bash shell设置下属于处理器ID:麻烦与BASH NAWK和OFS

这里是我想要的输出从提取:($输出的内容)

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从本教程中获得的:tutorial link

回答

1

ORS默认情况下已被设置为"\n"。由于您想要使用多个打印语句,因此您需要将其设置为空字符串,因为在任何打印语句之后都有一个隐含的print ORS

print $output | awk ' 
    BEGIN { ORS=""; } 
    { 
     print $4; 
     for (i=6;i<=NF;i++) 
      print " " $i; 
     print "\n"; 
    }' 

你也可以用切不要这样:

print $output | cut -d ' ' -f 4,6- 
+0

感谢约翰...这是一个真棒抓......我凭着了解隐含打印ORS ... – tomkaith13 2011-05-06 22:29:48

1

试试这个

print $output | nawk '         
BEGIN { ORS="\n" ; OFS = " " } 
{ 
outrec = "" 
for (i=6;i<=NF;i++) 
    outrec = outrec " " $i 
    print $4 " " outrec 
}'