2行にわたるパターンの変更

2行にわたるパターンの変更

NSedエディタでオプションがどのように機能するかを理解したいと思います。私の目標は変わります。「システム管理者」到着「デスクトップユーザー」内部に「ファイル01」、ブレーキラインは最後の行にもあります。 sed は次の行がないため、最後の行をキャッチしません。以下を追加するなど、修正する必要があるもう1つのことがあります。

sed 's/System Administrator/Desktop User/'しかし、これと:

sed 'System\nAdministrator/Desktop\nUser/'コマンドのいずれかが最後の行または2行の動作を停止するように予期しない方法に切り替えます。これはN2人の間で、または2人の前で起こります。私はGNU Sedバージョン4.4を使用しています。

#cat file01
The first meeting of the Linux System
Administrator's group will be held on Tuesday.
Another line
And here we have: System Administrator's Group as well.
1.System Administrator's group.
2.System Administrators Group.
3.System Administrators Group.
The first meeting of the Linux System
Administrator's group will be held on Tuesday.
System Administrators Group.

ケース1、次の2つのコマンドN

# sed '
>N
>s/System\nAdministrator/Desktop\nUser/
>s/System Administrator/Desktop User/
> ' file01
The first meeting of the Linux Desktop
User's group will be held on Tuesday.
Another line
And here we have: Desktop User's Group as well.
1.Desktop User's group.
2.System Administrators Group.
3.Desktop Users Group.
The first meeting of the Linux System
Administrator's group will be held on Tuesday.
Desktop Users Group.

ケース2sed 's/System Administrator/Desktop User/'これからN

# sed '
> s/System Adminitrator/Desktop User/
> N
> s/System\nAdministrator/Desktop\nUser/
> ' file01
The first meeting of the Linux Desktop
User's group will be held on Tuesday.
Another line
And here we have: System Administrator's Group as well.
1.Desktop User's group.
2.System Administrators Group.
3.Desktop Users Group.
The first meeting of the Linux System
Administrator's group will be held on Tuesday.
System Administrators Group.

これは私にとって奇妙に見え、何が間違っているのかわかりません。 [編集]:詳細。

代替案を探しています。「システム管理者」そして「デスクトップユーザー」。また、1行が「システム」で終わり、次の行が「管理者」で始まる場合は、それに応じて「デスクトップ」と「ユーザー」に変更します。これらのすべては本からのものですが、結果は本の内容と一致しません。結局何が間違っているのかわかりません。私の問題を説明するために私が見つけた唯一の世界は優先順位です。お詫び申し上げます。私が間違っているようです。

ベストアンサー1

これは実際には優先順位(通常は演算子に関連)とは関係なく、むしろコマンドが実行される順序に関連しています。


質問の最初の例を見てください。

N
s/System\nAdministrator/Desktop\nUser/
s/System Administrator/Desktop User/

その後、行をペアで読み取り、2つの代替項目を適用します。ペアの2行目がSystemAdministrator次の3行目)で終わると、検出されません。これは、文字列が奇数行と偶数行にまたがっても置き換えられないことを意味します。

質問の2番目の例を見てください(スペル修正)。

s/System Administrator/Desktop User/
N
s/System\nAdministrator/Desktop\nUser/

これにより、現在の行の文字列が変更され、次の行が読み取られ、途中で改行文字を含む文字列が変更されます。 文字列の完全なコピーで奇数行を変更しません。(または奇数行のみSystem)。


GNUの使用sed:

:top
N
$s/System\(.\)Administrator/Desktop\1User/g
b top

スクリプトはファイル内のすべての行を繰り返しパターン空間に読み込みます。入力の最後の行に達すると、2つの単語の間にすべての文字を許可しながら、グローバルに置換を実行します(代わりに\([ \n]\))を使用することもできます\(.\)

結果は次のとおりです。

The first meeting of the Linux Desktop
User's group will be held on Tuesday.
Another line
And here we have: Desktop User's Group as well.
1.Desktop User's group.
2.Desktop Users Group.
3.Desktop Users Group.
The first meeting of the Linux Desktop
User's group will be held on Tuesday.
Desktop Users Group.

おすすめ記事