各パターンマッチの前に5行のテキストを挿入する

各パターンマッチの前に5行のテキストを挿入する

同様の文脈で、パターン(例えば、RotX)が複数回繰り返されるファイルがあります。各行の先頭(各パターンマッチの前に5行)に特定のテキスト(例えば、Rot-X)を挿入する必要があります。

...

_face_641
{
    type wall;
    nFaces 6;
    startFace 63948413;
    inGroups       1(RotX);
}

_face_821
{
    type wall;
    nFaces 3;
    startFace 63948419;
    inGroups       1(RotX);
}

_face_67
{
    type wall;
    nFaces 3;
    startFace 63948422;
    inGroups       1(RotX);
}

...

しなければならない

...

Rot-X_face_641
{
    type wall;
    nFaces 6;
    startFace 63948413;
    inGroups       1(RotX);
}

Rot-X_face_821
{
    type wall;
    nFaces 3;
    startFace 63948419;
    inGroups       1(RotX);
}

Rot-X_face_67
{
    type wall;
    nFaces 3;
    startFace 63948422;
    inGroups       1(RotX);
}

...        

sedまたはawkを使用してこれを実行できますか?

助けてくれてありがとう。

ベストアンサー1

簡単な2段階の方法を使用してください。

$ awk 'NR==FNR{ if (/RotX/) nrs[NR-5]; next } FNR in nrs{ $0="Rot-X" $0 } 1' file file
...

Rot-X_face_641
{
    type wall;
    nFaces 6;
    startFace 63948413;
    inGroups       1(RotX);
}

Rot-X_face_821
{
    type wall;
    nFaces 3;
    startFace 63948419;
    inGroups       1(RotX);
}

Rot-X_face_67
{
    type wall;
    nFaces 3;
    startFace 63948422;
    inGroups       1(RotX);
}

...

おすすめ記事