提取行的范围,使用SED重叠 [英] Extract a range of rows, with overlap using sed
问题描述
我有一个(虚拟)文件,该文件是这样的:
I have a (dummy) file that looks like this:
header
1
2
3
4
5
6
7
8
9
10
和我需要一个命令,会给我做每一个四行一个overlaping提取一行行不同的文件。所以,我有这样的事情:
And I need a command that would give me different files made of rows extracted every four lines with one overlaping row. So I would have something like this:
1
2
3
4
3
4
5
6
5
6
7
8
7
8
9
10
因此,这里是我得到了什么(这是不是很多,不好意思):
So here is what I got (it is not much, sorry):
尾-n + 2 | SED -n 1,4p> window1.txt
tail -n + 2 | sed -n 1,4p > window1.txt
但我不知道如何对所有文件应用此,与重叠。
But I don't know how to apply this over all the file, with an overlap.
先谢谢了。
推荐答案
这可能会为你工作(GNU sed和分割):
This might work for you (GNU sed and split):
sed -nr '1{N;N;N};:a;p;$q;s/^.*\n.*\n(.*\n.*)$/\1/;N;N;ba' file | split -dl4
编辑:
为了使这个可编程使用:
To make this programmable use:
sed -nr ':a;$!{N;s/[^\n]+/&/4;Ta};p;$q;s/.*((\n[^\n]*){2})$/\1/;D' file |
split -dl4 file-name-prefix
其中, 4
是每个文件中的行数和 2
是重叠的行数。
Where 4
is the number lines per file and 2
is the number of overlap lines.
文件的名称 - preFIX
是您选择的文件名,这将有附加数字(见的人分割)。
File-name-prefix
is your chosen file name which will have numbers appended (see man split).
这篇关于提取行的范围,使用SED重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!