行の先頭の「-from」を「this」に置き換えます。

行の先頭の「-from」を「this」に置き換えます。

行の先頭の「-from」を「this」に変更したいと思います。行の末尾に「R」があり、その上の行の末尾に「D」がある場合、これが発生します。

たとえば、以下に示すブロックは次のとおりです。

-from XXXXXXXXXXXXXXXXX/D   
-from XXXXXXXXXXXXXXXXX/R   
-from XXXXXXXXXXXXXXXXX/K   
-from XXXXXXXXXXXXXXXXX/L   
-from XXXXXXXXXXXXXXXXX/G   
-from XXXXXXXXXXXXXXXXX/R 

出力は次のようになります。

-from XXXXXXXXXXXXXXXXX/D   
-this XXXXXXXXXXXXXXXXX/R   
-from XXXXXXXXXXXXXXXXX/K   
-from XXXXXXXXXXXXXXXXX/L   
-from XXXXXXXXXXXXXXXXX/G   
-from XXXXXXXXXXXXXXXXX/R  

すべて大丈夫、、、、sedなどawkgrep

ベストアンサー1

  • 前の行がD終わったら、
    • 現在行がR終わったら、
      • 次に、最初の単語(-from)をに置き換える必要があります-this

awkスクリプト:

# if the prev. line ended with D, and the current with R, replace first word
# optionally add && $1 == "-from"
has_d && /R$/ { $1 = "-this"; }
# print the current line, pretend that d is not matched yet
{ print; has_d = 0; }
# if line ends with D, set flag
/D$/ { has_d = 1; }

短い冗談:

awk 'has_d&&/R$/{$1="-this"}{print;has_d=0}/D$/{has_d=1}' yourfile

おすすめ記事