Case 1
We take into account two cases based on different requirements.
The first case is examining
every line of a file.
If a line contains 'PAT', then print the next line.
|
| Raw Input
|
| Desired Output
| Line 1
This line contains PAT
Line 3
Line 4
PAT is here
One more line contains PAT
Two more line contains PAT
Line 8
This has PAT, too
Line 10
|
| Line 3
One more line contains PAT
Two more line contains PAT
Line 8
Line 10
|
|
Script and Comments
Script1 [ 1] :loop
[ 2] $!N
[ 3] /PAT.*\n/!D
[ 4] s/^.*\n//p
[ 5] b loop
|
Script2 [ 1] $!N
[ 2] /PAT.*\n/!D
[ 3] s/^.*\n\(.*\)/\1\n\1/
[ 4] P
[ 5] D
| |
Case 2
A line will be printed if it does not contain 'PAT' and is preceded by
an adjacent line containing 'PAT'.
|
| Raw Input
|
| Desired Output
| Line 1
This line contains PAT
Line 3
Line 4
PAT is here
One more line contains PAT
Two more line contains PAT
Line 8
This has PAT, too
Line 10
|
| Line 3
Line 8
Line 10
|
|
Script and Comments
Script1 [ 1] /PAT/!d
[ 2] $!N
[ 3] /\n.*PAT/D
[ 4] s/^.*\n//
| |