如何在awk中抑制换行

如何在awk中抑制换行,awk,Awk,虽然我在下面处理一些awk脚本,但它会生成许多不需要的LF。我想知道如何在这个脚本中抑制LF的输出。提前感谢你的善意建议 awk ' BEGIN{ FS=OFS="," } { nf=NF s=0 # initialization if($1==3){print "1,"; s+=50}else{print "0,"} if($2==1){print "1,";

虽然我在下面处理一些awk脚本,但它会生成许多不需要的LF。我想知道如何在这个脚本中抑制LF的输出。提前感谢你的善意建议

awk '
BEGIN{
    FS=OFS=","
}
{
    nf=NF
    s=0 # initialization
        if($1==3){print "1,"; s+=50}else{print "0,"}
        if($2==1){print "1,"; s+=50}else{print "0,"}
        print s
        print ","
}END{}' file
文件的内容类似于

3,1
3,2
在本例中,输出如下

1,
1,
100
,
1,
0,
50
,
那太笨拙了。我想要的结果是

1,1,100
1,0,50

请你试试下面,修正OP的尝试。在GNU awk中更正和测试

OP尝试中的修复:

已删除nf变量,但不需要它。 我们不需要在满足条件后立即打印,这就是为什么输出中出现问题的原因,相反,我们可以将值保存到fieldseg->1st和2nd字段中,最后使用变量打印行。 说明:增加对以上内容的详细说明

awk '                                       ##Starting awk program from here.
BEGIN{                                      ##Starting BEGIN section of this program from here.
    FS=OFS=","                              ##Setting FS and OFS as comma here.
}
{
   s=0                                      ##Setting s as 0 here.
   if($1==3){s+=50;$1="1"} else{$1="0"}     ##Checking if 1st field is 3 then add 50 to s and set 1 to $1 else keep $1 as 0 here.
   if($2==1){s+=50;$1="1"} else{$2="0"}     ##Checking if 2nd field is 3 then add 50 to s and set 1 to $2 else keep $2 as 0 here.
   print $0,s                               ##Printing line and value of s here.
}' Input_file                               ##mentioning Input_file name here.

请你试试下面,修正OP的尝试。在GNU awk中更正和测试

OP尝试中的修复:

已删除nf变量,但不需要它。 我们不需要在满足条件后立即打印,这就是为什么输出中出现问题的原因,相反,我们可以将值保存到fieldseg->1st和2nd字段中,最后使用变量打印行。 说明:增加对以上内容的详细说明

awk '                                       ##Starting awk program from here.
BEGIN{                                      ##Starting BEGIN section of this program from here.
    FS=OFS=","                              ##Setting FS and OFS as comma here.
}
{
   s=0                                      ##Setting s as 0 here.
   if($1==3){s+=50;$1="1"} else{$1="0"}     ##Checking if 1st field is 3 then add 50 to s and set 1 to $1 else keep $1 as 0 here.
   if($2==1){s+=50;$1="1"} else{$2="0"}     ##Checking if 2nd field is 3 then add 50 to s and set 1 to $2 else keep $2 as 0 here.
   print $0,s                               ##Printing line and value of s here.
}' Input_file                               ##mentioning Input_file name here.
可读性更好:

awk 'BEGIN{
       FS=OFS=","
     }
     {
        c1=c2=sum=0;          # reset variables
     }
     $1==3{                   # if col1 equal to 3 then
        c1=1;
        sum+=50
     }
    $2==1{                    # if col2 equal to 1 then
       c2=1;
       sum+=50
    }
    {
        print c1,c2,sum       # print variables 
    }' input    
可读性更好:

awk 'BEGIN{
       FS=OFS=","
     }
     {
        c1=c2=sum=0;          # reset variables
     }
     $1==3{                   # if col1 equal to 3 then
        c1=1;
        sum+=50
     }
    $2==1{                    # if col2 equal to 1 then
       c2=1;
       sum+=50
    }
    {
        print c1,c2,sum       # print variables 
    }' input    
您可以通过将ORS设置为来告诉AWK不要在每次打印后放置换行符,但是您需要自己在需要的地方放置换行符。您的代码可能会按照以下方式重新编写:

awk '
BEGIN{
    ORS="";FS=OFS=","
}
{
    nf=NF
    s=0 # initialization
        if($1==3){print "1,"; s+=50}else{print "0,"}
        if($2==1){print "1,"; s+=50}else{print "0,"}
        print s
        print "\n"
}END{}' file
那么对于文件内容

3,1
3,2
它将输出:

1,1,100
1,0,50
在gawk 4.2.1中测试,您可以通过将ORS设置为来告诉AWK不要在每次打印后添加换行符,但是您需要自己在需要的地方添加换行符。您的代码可能会按照以下方式重新编写:

awk '
BEGIN{
    ORS="";FS=OFS=","
}
{
    nf=NF
    s=0 # initialization
        if($1==3){print "1,"; s+=50}else{print "0,"}
        if($2==1){print "1,"; s+=50}else{print "0,"}
        print s
        print "\n"
}END{}' file
那么对于文件内容

3,1
3,2
它将输出:

1,1,100
1,0,50
在gawk 4.2.1中测试