Shreya
Shreya

Reputation: 649

Add rows from another file when condition is true

With continuation to the question in link Print rows with condition on field data

I have another file2 with data

cell   input  out  type   fun  level
CLK    C       Z    seq   Cq   1      
DFk    C,Cp    Q    seq   IQ   1
DFR    D,C     Qn   seq   IN   1
SKN    SE,Q    Qp   seq   Iq   1

the below code is to get level sum as 7 and printing the rows.

awk '
function rnd(max) {        
   return int(rand()*max-1)+2
}
BEGIN {
   srand()                
}
NR == 1 {                  
   print                   
   next
}
{
   rec[NR] = $0           
   num[NR] = $NF           
}
END {
   while(1) {             
      r = rnd(NR)          
      if (!seen[r]++)      
         s += num[r]      

      if (s == 7)         
         break
      else if (s > 7) {   
         delete seen
         s = 0
         continue
      }
   }
   for (j in seen)         
      print rec[j]
} ' file1

The above code prints only one set of output( level sum 7 true) each time when i execute it.

But now I want to print two set of data which results in level sum as 7 when I execute the code once and whenever level sum = 7 is true, i want to insert lines from file2 to it in new line.

output resulting in

cell   input     out    type      fun            level
AI20   A1,A2      Z     comb    ((A1A2))           2
INV    I1         ZN    comb    (!I1)              1  
BUF  A1,A2,A3,B1  Z     comb    (!(((A1A2)A3)B1))  4
CLK    C          Z     seq      Cq                1
XOR    A1,A2,B1   Z     comb    (((A1A2)B1)        3
IAD    A1,A2,A3   Z     comb    (!((A1A2)A3))      3
INV    I1         ZN    comb    (!I1)              1

Here in output we can see that CLK C Z seq Cq 1 line is inserted from file2 when level sum above it for file1 was 7.

To get this output I modified the above code as

awk '
function rnd(max) {        
   return int(rand()*max-1)+2
}
BEGIN {
   srand()                
}
NR == 1 {                  
   print                   
   next
}
{
   rec[NR] = $0           
   num[NR] = $NF           
}
END {
   for (( i=1; i<3;i++)) 
     do  
         
      r = rnd(NR)          
      if (!seen[r]++)      
         s += num[r]      

      if (s == 7)         
         for line in $file2
           do
             awk '{ $0 }'
           done
     done

      else if (s > 7) {   
         delete seen
         s = 0
         continue
      }
   
   for (j in seen)         
      print rec[j]
} ' file1

but I am getting error while printing line from file2.

Upvotes: 1

Views: 81

Answers (1)

anubhava
anubhava

Reputation: 785058

You may use this enhanced awk script:

cat rnd.awk

function rnd(max) {           # generate a randon number between 2 and max
   return int(rand()*max-1)+2
}
function rndsum() {
   while(1) {                 # infinite loop
      r = rnd(NR)             # generate a randomm number between 2 and NR
      if (!seen[r]++)         # populate seen array with this random number
         s += num[r]          # get aggregate sum from num array

      if (s == sum)           # if sum then break the loop
         break
      else if (s > sum) {     # if s > sum then restart the loop
         delete seen
         s = 0
         continue
      }
   }
   for (j in seen)            # for each val in seen print rec array
      print rec[j]
   delete seen
   s = 0
}
BEGIN {
   srand()                    # seed random generation
}
NR == 1 {                     # for header row
   print                      # print header record
   next
}
FNR == NR {                   # while processing file1
   rec[NR] = $0               # save each record in rec array with NR as key 
   num[NR] = $NF              # save last column in num array with NR as key
   next
}
FNR > 1 {
   rec2[++n] = $0             # records in file2 being saved in rec2
}
END {
   rc = rc > n ? n : rc       # allow rc to be max of row count in file2
   for (i=1; i<=rc; ++i) {    # for 1..rc
      rndsum()               # print random rows from file1
      print rec2[i]           # print a row from file2
   }
   rndsum()                  #  print random rows from file1
}

Now use it as:

awk -v sum=7 -v rc=2 -f rnd.awk file1 file2 | column -t

cell  input        out  type  fun                level
IA2   A1,A2,A3     Z    comb  ((!A1A2)A3)        3
BUF   A1,A2,A3,B1  Z    comb  (!(((A1A2)A3)B1))  4
CLK   C            Z    seq   Cq                 1
IA2   A1,A2,A3     Z    comb  ((!A1A2)A3)        3
IAD   A1,A2,A3     Z    comb  (!((A1A2)A3))      3
INV   I1           ZN   comb  (!I1)              1
DFk   C,Cp         Q    seq   IQ                 1
XOR   A1,A2,B1     Z    comb  (((A1A2)B1)        3
IAD   A1,A2,A3     Z    comb  (!((A1A2)A3))      3
INV   I1           ZN   comb  (!I1)              1

and again:

awk -v sum=7 -v rc=1 -f rnd.awk file1 file2 | column -t

cell  input        out  type  fun                level
IAD   A1,A2,A3     Z    comb  (!((A1A2)A3))      3
BUF   A1,A2,A3,B1  Z    comb  (!(((A1A2)A3)B1))  4
CLK   C            Z    seq   Cq                 1
XOR   A1,A2,B1     Z    comb  (((A1A2)B1)        3
BUF   A1,A2,A3,B1  Z    comb  (!(((A1A2)A3)B1))  4

Upvotes: 2

Related Questions