我有两个不同的文件,以Tab
. 文件 1 如下所示:
transcr_15824 3.95253441295071 3.99992738843234 3.93880798313547
YML042W 10.3143219248979 10.6898819949325 11.0073811719421
transcr_18545 7.76182774638543 7.25508954643215 7.92562682485731
YCR105W 8.46144110056843 8.30995100411912 8.85470858413405
transcr_18545 7.76182774638543 7.25508954643215 7.92562682485731
YMR325W 6.2822794040082 6.46992587787936 7.00507748994596
Run Code Online (Sandbox Code Playgroud)
文件 2 如下所示:
YLR177W 11.321823973245 12.1264440368589 11.7777091957438
YOR117W 10.7514234580732 11.3932687209745 11.2587694561818
TY_120 5.95114867088525 5.93580053538449 5.89166059690558
YMR174C 8.49545850099485 8.72467418433346 9.6518559706269
YPL117C 10.7211879012765 10.5046713289602 10.6145538571844
TY2_LTR_77 11.9297940548212 11.9801206538102 12.049127298122
YOL101C 7.76141097131674 9.89522697916433 7.85466704627526
YLR053C 7.62843998411388 7.49205634213499 7.10263942962051
YBR135W 9.70614244227352 9.3114074341804 9.36413815370247
YNL168C 9.93928326709444 10.3036524361223 10.0704544058998
Run Code Online (Sandbox Code Playgroud)
我试图现在要做的就是从加10号线File 2
到File 1
后2行。它应该是这样的:
transcr_15824 3.95253441295071 3.99992738843234 3.93880798313547
YML042W 10.3143219248979 10.6898819949325 11.0073811719421
YLR177W 11.321823973245 12.1264440368589 11.7777091957438
YOR117W 10.7514234580732 11.3932687209745 11.2587694561818
TY_120 5.95114867088525 5.93580053538449 5.89166059690558
YMR174C 8.49545850099485 8.72467418433346 9.6518559706269
YPL117C 10.7211879012765 10.5046713289602 10.6145538571844
TY2_LTR_77 11.9297940548212 11.9801206538102 12.049127298122
YOL101C 7.76141097131674 9.89522697916433 7.85466704627526
YLR053C 7.62843998411388 7.49205634213499 7.10263942962051
YBR135W 9.70614244227352 9.3114074341804 9.36413815370247
YNL168C 9.93928326709444 10.3036524361223 10.0704544058998
transcr_18545 7.76182774638543 7.25508954643215 7.92562682485731
YCR105W 8.46144110056843 8.30995100411912 8.85470858413405
Run Code Online (Sandbox Code Playgroud)
所以,基本上,我试图从移动10行File 2
每间transcr_
保持已经存在的行已经低于每transcr_
。
编辑:
File 2
大约有 2,000 行,File 1
大约有 200 个“transcr_”行。所以,这将是:拿起 的前 10 行File 2
,将它们放在第一和第二个“transcr_”行之间(以及在这两个“transcr_”之间已经存在的行之后。然后,从 11 到 20 中获取行File 2
并将它们放在第二个和第三个“transcr_”之间。然后,从 21 到 30 中取出行File 2
并将它们放在第三个和第四个“transcr_”之间,依此类推。
它可能看起来像这样:
transcr_1
already existing line
10 first lines from `File 2`
transcr_2
already existing line
Lines 11-20 from `File 2`
transcr_3
already existing line
Lines 21-30 from `File 2`
transcr_4
.....
Run Code Online (Sandbox Code Playgroud)
Jef*_*ler 11
你可以用ed
!
ed -s file1 <<< $'2r !head -10 file2\nw\nq'
Run Code Online (Sandbox Code Playgroud)
这告诉 ed 使用三个命令编辑 file1:
head -10 file2
并插入w
写出文件q
适合使用 GNU sed(使用e
扩展,它从 shell 命令输入输入):
sed -i '3e head -10 file2' file1
Run Code Online (Sandbox Code Playgroud)
下面的脚本是一个for
循环,它重复这个ed
想法的次数与transcr_
file1中的块一样多。每次通过循环,我们计算三个项目:
ed
从文件 1 开始读取的行号sed
从 file2 开始读取的行号sed
停止从 file2 读取的行号第 1 项更清楚地拼写为:10*(N-1) + 2*N
,我将其简化为12*N - 10
。
第 2 项和第 3 项更清楚地拼写为10*(N-1) + 1
through 10*N
,我将其简化为10*N - 9
through 10*N
。
我用head
更灵活和更强大的sed
命令替换了该命令,用于从 file2 中挑选出行块。
这将times
在循环时重写 file1次。
# how many times we need to insert blocks
times=$(grep -c transcr_ file1)
for((index=1;index <= times; index++));
do
printf "%dr !sed -n %d,%dp file2\nw\nq\n" $((12 * index - 10)) $((10 * index - 9)) $(( 10 * index )) |
ed -s file1
done
Run Code Online (Sandbox Code Playgroud)