在字段数据上打印带有条件的行 [英] Print rows with condition on field data
问题描述
我有包含数据的文件
cell input out type fun level
AI20 A1,A2 Z comb ((A1A2)) 2
IA2 A1,A2,A3 Z comb ((!A1A2)A3) 3
XOR A1,A2,B1 Z comb (((A1A2)B1) 3
IAD A1,A2,A3 Z comb (!((A1A2)A3)) 3
INV I1 ZN comb (!I1) 1
BUF A1,A2,A3,B1 Z comb (!(((A1A2)A3)B1)) 4
根据此数据,我要打印其
level字段(第6列)
在一起给出sum 7
的行.
在这里获得 level sum
7
,我们可以选择 AI2O
, BUF
, INV 代码>给出级别总和的行为
2
+ 4
+ 1
= 7
,并打印它们 Or
可以选择 XOR
, IAD
, INV
和和 3
+ 3
+ 1
= 7
并打印出来.任何随机选择的行都可以,但是 level sum
必须为 7
here to get level sum
7
we can select AI2O
,BUF
,INV
rows giving level sum as 2
+4
+1
=7
and print them
Or
can select XOR
,IAD
,INV
giving sum 3
+3
+1
=7
and print them. Any random selection of rows work but level sum
needs to be 7
输出可以为
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
或者输出也可以是
cell input out type fun level
XOR A1,A2,B1 Z comb (((A1A2)B1) 3
IAD A1,A2,A3 Z comb (!((A1A2)A3)) 3
INV I1 ZN comb (!I1) 1
我使用awk尝试了
awk '{{ sum[i] += $6} for (i=1;i<8;i++) print $0}' file
但这将每行打印7次,而不是所需的输出.
But this is printing each row 7 times not the desired output.
第2部分.Prblm继续第1部分.
Part 2. Prblm continue to part 1.
带有数据的文件2
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
要获取第2部分的输出
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
DFk C,Cp Q seq IQ 1
IA2 A1,A2,A3 Z comb ((!A1A2)A3) 3
XOR A1,A2,B1 Z comb (((A1A2)B1) 3
INV I1 ZN comb (!I1) 1
第2部分的输出是,当我们获得文件1的级别总和为7时,在文件2之后插入第一行.并再次检查级别总和7的条件,如果为true,则从file2插入第二行.然后再次检查级别总和为7.如果为true,则从file2插入第三行.完成执行3次.
output for part2 is that when we get level sum as 7 for file1, insert first line from file2 after it. And again check for condition for level sum 7 and if true insert second line from file2. Then again check for level sum as 7. If true insert 3rd line from file2. This is done for execution 3 times.
推荐答案
以下是该工作的awk解决方案:
Here is an awk solution for this job:
cat rnd.awk
function rnd(max) { # generate a randon number between 2 and max
return int(rand()*max-1)+2
}
BEGIN {
srand() # seed random generation
}
NR == 1 { # for header row
print # print header record
next
}
{
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
}
END {
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 == 7) # if sum is 7 then break the loop
break
else if (s > 7) { # if sum > 7 then restart the loop
delete seen
s = 0
continue
}
}
for (j in seen) # for each val in seen print rec array
print rec[j]
}
用作:
awk -f rnd.awk file
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
再一次:
awk -f rnd.awk file
cell input out type fun level
IA2 A1,A2,A3 Z comb ((!A1A2)A3) 3
XOR A1,A2,B1 Z comb (((A1A2)B1) 3
INV I1 ZN comb (!I1) 1
这篇关于在字段数据上打印带有条件的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!