文件我有这将有以下模式拆分基于图案
HDR1|20160101|1234|
N1|ABC|
XXX|21431415|3522352352|ITEM|
FORE|20140508|20140214|
SD|0|0039 - data|data|data|data|
SD|0|0211 - data|data|data|data|
SD|0|0039 - data|data|data|data|
SD|0|0211 - data|data|data|data|
FORE|20140508|20140214|
SD|0|0039 - data|data|data|data|
SD|0|0039 - data|data|data|data|
SD|0|0211 - data|data|data|data|
我想分裂基于尺寸的文件,但还需要采取以下护理文件。
前3行是标题,我需要将其包含在我创建的每个分割文件中。 以FORE开头的行与SD之后的行有关系,所以我必须将它们放在一起。
输出应如下所示。
分割文件1:
HDR1|20160101|1234|
N1|ABC|
XXX|21431415|3522352352|ITEM|
FORE|20140508|20140214|
SD|0|0039 - data|data|data|data|
SD|0|0211 - data|data|data|data|
SD|0|0039 - data|data|data|data|
SD|0|0211 - data|data|data|data|
分割文件2:
HDR1|20160101|1234|
N1|ABC|
XXX|21431415|3522352352|ITEM|
FORE|20140508|20140214|
SD|0|0039 - data|data|data|data|
SD|0|0039 - data|data|data|data|
SD|0|0211 - data|data|data|data|
我建立了一个伪代码,它看起来像below.There可多套这样的船头和SD我”的已经保持在一起为一组,所以我已经把一个循环
create $file
create $line_num=5
create $file_size
create $top_size=20mb
read the first 4 lines of the original file and copy it in a temphdr file
Loop until last $line_num is encountered
read the header details and Append the header from the temphdr to the $file
for each $record starting the head -$line_num (5,6,7...etc) that contains FORE| in the first part
if the $file size is < $top_size
append the $record in the $file
increment $line_num
For each $record in head -$line_num that contains SD| in the first part
append the $record in the $file
increment $line_num
else
create a $file=$file+1
fi
end loop
end loop
可能有人让我知道是否有任何其他影响一种使用awk和sed等的方式来实现这一点,而不是上面提到的高级逻辑。
我注意到你最近的编辑 - 你有理由相信我的答案*不*已经做你要求的东西(重新:分裂只在一个FORE,保持作为一套与以下SDS)? –
嘿查尔斯。对于那个很抱歉。我起初没有注意到它,所以我继续编辑我的问题,认为我可能没有详细阐述,但是后来立即注意到你已经考虑了我的观点。 :)。我为此感谢你.. – user3055262
NP。唯一需要解释的问题是你是否想要在* FORE(如@ anubhava的答案)中分割,或者在20mb边界之后的第一个FORE中分割(就像我的答案一样)。 –