0

While I am conding some awk script below, but it generates many unwanted LFs. I wonder how to suppress (control) the output of LF in this script. Thanks in advance for your kind advice.

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

The content of the file (file) is something like,

3,1
3,2

In this case, the output is like

1,
1,
100
,
1,
0,
50
,

That is very clumsy. My desired output is like,

1,1,100
1,0,50
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
user15181
  • 115
  • 1
  • 6

3 Answers3

3

Could you please try following, fixing OP's attempt here. Corrected and tested in GNU awk.

awk '
BEGIN{
    FS=OFS=","
}
{
   s=0
   if($1==3){s+=50;$1="1"} else{$1="0"}
   if($2==1){s+=50;$1="1"} else{$2="0"}
   print $0,s
}' Input_file

Fixes in OP's attempts:

  • Removed nf variable its not needed.
  • We need NOT to print asap a condition gets satisfied that's why problem is coming in output, rather we can save values into the fields(eg--> 1st and 2nd fields) and print line at last with variable.

Explanation: Adding detailed explanation for above.

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.
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
3
$ cat input 
3,1
3,2

$ awk 'BEGIN{FS=OFS=","}{c1=c2=sum=0;}$1==3{c1=1;sum+=50}$2==1{c2=1;sum+=50}{print c1,c2,sum}' input 
1,1,100
1,0,50

Better Readable:

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    
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
0

You might tell AWK to not put newline after each print by setting ORS to "", but then you need to put newline yourself where needed. Your code might be reworked following way:

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

then for file content being

3,1
3,2

it will output:

1,1,100
1,0,50

(tested in gawk 4.2.1)

Daweo
  • 31,313
  • 3
  • 12
  • 25