一致する行の文字を置き換える

一致する行の文字を置き換える

ipを含む文字があります。変えたいすべての数ipには「SpecialWord」の後に他の文字はありません。各回線には複数のIPがあります。
たとえば、
この入力は試してみましたが、
*random text with different numbers* 255.43.23.8 *some more text* "SpecialWord" 32.123.21.44 *text again*
IPの正確な桁数を知らず、sedがプレビューを実行できないことを意味します。ここに私に役立つものはありますか?
*random text with different numbers* aaa.aa.aa.a *some more text* "SpecialWord" 32.123.21.44 *text again*

sed -r 's/([0-9]{1,3}\.){3}[0-9]{1,3}/.../g'

ベストアンサー1

正規表現がパターンの長さを記憶できるかどうかわからないので、純粋な(部分的な)sedソリューションの場合:

編集する:

これは少しトリッキーです。

私はsedファイルa.sedを思い出しました。

p
s/[0-9]\.[0-9]/a.a/g
s/with/phase 1/
p
s/[0-9]a\.a/aa.a/g
s/a\.a[0-9]/a.aa/g
s/phase 1/phase 2/
p
s/[0-9]aa.a/aaa.a/g
s/a.aa[0-9]/a.aaa/g
s/phase 2/final/

これはデフォルトではコマンドラインと同じですが、デバッグ行を使用します。

  • 最初の行( - 最初の行は次のとおりです(1.2テキストa.aの数字に注意してください。例1.12%:)a.a2%
  • 次の行では、2桁と3桁のIPを構築します。 )になります1.2a.aテキストの数字を参照してください。たとえば、1.12%willになりますa.a2%)。
  • 次の数行は2桁と3桁のIPです。
  • p削除後のライン確認後s/phase x/../

使用

sed -f a.sed u 

どこにuありますか(はい、有効なIPではありませんが、それはポイントではありません)

*random text with different numbers* 255.43.23.8 *some more text* "SpecialWord" 32.123.21.44 *text again*
*random text with different 12* 255.4.2.8 *some more text* "SpecialWord" 32.12.21.44 *text again*
*random text with different 13* 255.453.263.788 *some more text* "SpecialWord" 2.123.21.454 *text again*
*random text with different 1.12%* 255.43.23.8 *some more text* "SpecialWord" 32.123.21.44 *text again*
*random text with different numbers* 5.43.23.8 *some more text* "SpecialWord" 32.3.1.44 *text again*

これにより

*random text final different numbers* aaa.aa.aa.a *some more text* "SpecialWord" aa.aaa.aa.aa *text again*
*random text final different 12* aaa.a.a.a *some more text* "SpecialWord" aa.aa.aa.aa *text again*
*random text final different 13* aaa.aaa.aaa.aaa *some more text* "SpecialWord" a.aaa.aa.aaa *text again*
*random text final different a.aa%* aaa.aa.aa.a *some more text* "SpecialWord" aa.aaa.aa.aa *text again*
*random text final different numbers* a.aa.aa.a *some more text* "SpecialWord" aa.a.a.aa *text again*

編集2:一時ファイルの余裕がある場合

grep -Eo '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' u > v

IPリストのインポートv

sed s/[0-9]/a/g v > w

匿名IPに移動w

paste -d '/' v w | sed -e 's|^.*$|s/&/|'

IPを匿名IPに変換するsedファイルを生成します。

paste -d '/' v w | sed -e 's|^.*$|s/&/|' | sed -f - u

...ファイルに適用されます。

1.12%線を除くと、結果は上記と同じです。

*random text with different 1.12%* aaa.aa.aa.a *some more text* "SpecialWord" aa.aaa.aa.aa *text again*

一時ファイルがあり、クルーズミサイルでウサギを殺したいとしましょう。

おすすめ記事