2 番目のシナリオで使用する 1 つの方法GNU sed
ですが、少し複雑に思えます (完全にコメントされています)。
infile
次のコンテンツがあると仮定します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
および内容script.sed
:
## From first line until a line that matches the pattern (number ten in
## this example), save lines in buffer and print each one when there are
## more than three lines between one of them and the line with the pattern
## to search.
0,/10/ {
## Mark 'a'
:a
## If line matches the pattern break this loop.
/10/ {
bb
}
## Until the pattern matches, if more than three lines (checking '\n') are
## saved, print the oldest one and delete it, because I only want to save last
## three.
/\(\n[^\n]*\)\{3\}/ {
P
D
}
## Append next line to pattern space and goto mark 'a' in a loop.
N
ba
}
## It should never match (I think), but it's a sanity check to avoid the
## following mark 'b'.
bc
## Here we are when found the line with the pattern, so read next five six
## lines and delete all of them but the sixth. If end of file found in this
## process none of them will be printed, so it seems ok.
:b
N;N;N;N;N
N
s/^.*\n//
## Here we are after deleting both '-3' and '+5' lines from the pattern matched,
## so only is left to print the remainder of the file in a loop.
:c
p
N
s/^.*\n//
bc
10
コードの 5 行目と 11 行目のパターンを考慮して、このように実行します。必要に応じて変更してください。私の例では、行を削除する必要があります7,8,9,10,11,12,13,14,15
:
sed -nf script.sed infile
次の出力で:
1
2
3
4
5
6
16
17
18
19
20