开发者

Trouble with nawk and OFS in BASH

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


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 implicit print ORS after any print statement.

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

You could also do this with cut:

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


Try this

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜