あなたが最初のケースでは、取得する場所A>B
sed -n '2{h; :a; n; 4{p;x;bb}; H; ba}; :b; p' file
またはA<B
sed -n '2{h; d}; 4{p; x;}; p' file
、line 2
への移行line 4
のために、私はline B
へline A
を移動したい、試すことができます:へline 4
を移動しますline 2
goal identifier statement
forall statement
let statement 1
let statement 2
other statements
あなたは後者の場合には、取得:line 2
goal identifier statement
let statement 2
forall statement
let statement 1
other statements
説明
sed -n ' #silent option ON
2{ #if is line 2
h #Replace the contents of the hold space with the contents of the pattern space
:a #label "a"
n #fetch the next line
4{ #if is line 4
p #print line 4
x #Exchange the contents of the hold and pattern spaces
bb #goto "b"
}
H #appends line from the pattern space to the hold space, with a newline before it.
ba #goto "a"
}
:b #Label "b"
p #print
' file
EDIT
あなたはregex
を使用するための行を特定したい場合は、次のことができ
line 4
に移動し最初のコマンドを変更する
sed -n '/goal/{p;n;h;:a;n;/forall/{p;x;bb};H;ba};:b;p' file
これは興味深い問題であるためにはawkを使用する必要があります。それのStackOverflowの良い質問にするために、追加してくださいコードを使用してソリューションを実現しようとしました。[MCVE](http://stackoverflow.com/help/mcve)と[how-to-ask](http://stackoverflow.com/help/)を参照してください。どのような良い質問があるかについてのいくつかのガイドラインについては、いくつかのガイドラインのページを参照してください。 – ghoti