JM88
JM88

Reputation: 477

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

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):

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.

Thanks in advance.

Upvotes: 1

Views: 124

Answers (1)

potong
potong

Reputation: 58538

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

EDIT:

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

Where 4 is the number lines per file and 2 is the number of overlap lines.

File-name-prefix is your chosen file name which will have numbers appended (see man split).

Upvotes: 2

Related Questions