2011-05-06 74 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 
}'