现在,因为你想从鼻子里吃意大利面而不感到痛苦,所以我们将读懂你的想法并猜测你想做什么。我们有两种选择,一种是把意大利面磨成液体,另一种是张开嘴。
话虽如此,我猜测问题可能出在
file1 上,它是某种结构化数据,它包含一个名为 RID 的键值对。我不知道它是什么,或者它做什么,或者代表什么,但是从字里行间看,file2 有一个对应的条目。
现在,file2 也以某种方式结构化,看起来像是有一些键值对组合成某种形式的结构化块,并且有多个块。
现在的问题是,我们的最终目标是什么?阅读_between_the_lines_,我猜你想要整个块,当该块的键值对,RID 键,匹配某个条件时。这个对吗 ?
那么具体是什么条件呢?
我的猜测是,当 FILE1 中存在 BLOCK 的 RID 时,您想从 FILE2 中执行 SELECT WHOLE_BLOCK 行
那么我们有两个选择。假设您的 file2 小于 9000PetaBytes,并且条目数量也少于 IEEE 52 位 fp 可以处理的数量,请将其存储在内存中。
echo ""| awk '
function cmd( E, A, this,v){ A[0]=0;while((E |getline v)>0)A[A[0]+=1]=v;A["RETURN_CODE"]=close(E);}
function grep( o, re, p, B, this, a,v ){
B[0]=0;if(o~"-v"){while((getline v < p)>0){if(!match(v,re))B[B[0]+=1]=v;}return B[0];};
if(o~"-o"){while((getline v < p)>0){a=v;while(match(a,re)){B[B[0]+=1]=substr(a,RSTART,RLENGTH);
a=substr(a,RSTART+RLENGTH);}};return B[0];};while((getline v < p)>0){if(match(v,re))B[B[0]+=1]=v;}return B[0];
}
function dbg_printarray(ary , x , s,e, this , i ){x=(x=="")?"A":x;for(i=((s)?s:1);i<=((e)?e:ary[0]);i++){print x"["i"]=["ary[i]"]"}}
function agrep( o, re, A, B, this, a, i,k ){
B[0]=0;k=0;if(o~"-v"){for(i=1;i<=A[0];i++){if(!match(A[i],re)) B[k+=1]=A[i];}B[0]=k;return k;};
if(o~"-o"){for(i=1;i<=A[0];i++){a=A[i];while(match(a,re)){B[B[0]+=1]=substr(a,RSTART,RLENGTH);a=substr(a,RSTART+RLENGTH);};
};B[0]=k;return k;};for(i=1;i<=A[0];i++){if(match(A[i],re))B[k+=1]=A[i];};B[0]=k;return k;
}
{
SAFETY_SINCE_WE_WALK_IN_THE_DARK=2000;
input="file1";
lookup_file="file2";
output="output.data";
we_died = 0;
# Instead of -i option to zgrep, use [Aa][Nn][Tt] way of representation.
PATTERN = "[Rr][Ii][Dd]=[0-9a-zA-Z]*";
if(for_gods_sake_we_are_scanning_normal_uncompressed_content){
grep("-o", PATTERN , input , A);
}else{
cmd("zgrep -o \""PATTERN"\" \""input"\" ",A);
}
# Now, A has matching data. A[0] holds total. A[1] to A[A[0]] holds data.
# Lets read lookup_file, block at a time.
# Since you did not give any specific caracteristics of file2, we can not optimeze in any way.
# Oh well.
while((getline v < lookup_file)>0){
# Throw away head until we reach a valid block header
if(v!~"^Starting of block ") continue;
# We are inside block.
blockid = substr(r,match(r,"[0-9]*$"));
# get whatever data inside block untill we reach end
c=0;
delete B;
B[0]=0;
B[B[0]+=1]=blockid;
while(((getline v < lookup_file)>0)&&v!~"^Ending of block" && c < SAFETY_SINCE_WE_WALK_IN_THE_DARK){
B[B[0]+=1]=v;
if(v~"RID"){
# store it so we can later play with it
B["RID"]=v;
};
c++;# We are fucked as the structure EOB was missing.
}
# we ither died, or end of block.
if(c >= SAFETY_SINCE_WE_WALK_IN_THE_DARK){
we_died = 1;
break;
}
# We assume B has whole block. B[0] has total. B[1] .. B[B[0]] has data. B["RID"] has RID for fast reference.
# Now, since the data format of file2 is not explained at all, I am guessing
# A[n] == "RID=DEADBEEF"
# and
# B["RID"] == "RID=DEADBEEF"
# holds true, which is totally unlikely. what if it is "RID = \t\t\t DeadBeEf"
# so this is really impossible to guess as the OP is not even sure what format they are using.
# sub("^[Rr][Ii][Dd][ \t]*=[ \t]*","",B["RID"])
# or something should be done so we can compare the damn thing.
matched_block = 0; matched_idx = 0;
for(i=1;i<=A[0];i++){
if(A[i]==B["RID"]){matched_idx = i; matched_block=1; break;}
}
if(matched_block){
# This block in B[] also matches A[matched_idx];
# Do what ever you want to do with it.
dbg_printarray(B,"B");
print "A["matched_idx"]=["A[matched_idx]"]";
print "Have fun";
}
}
}'
我不得不说,如果没有清楚地了解您想要实现的目标,就可以帮助您。下一次,不要认为你是提出逻辑的最佳人选。一般来说,有些专家专门创建逻辑,而我们大多数人会选择适合我们问题的东西。试图解决一个问题并不意味着你必须重新发明轮子。只需描述你拥有什么,以及你想要实现什么。很有可能,有人已经做到了,而且很可能有一个完整的框架和胶带将东西粘在一起。