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
В sed
:
sed '/D$/{N;/R$/s/\n-from/\n-this/}' your_file
Расширенный с комментариями:
sed ' /D$/{ # If the current line ends in D
N; # Append the next line to the pattern space
/R$/s/\n-from/\n-this/ # If you find R at end-of-line, substitute
}' your_file