Это будет выглядеть немного некрасиво, но на самом деле не очень сложно.Используя sed:
sed '1h; 1!H; $!d; x; s@\(const/4 v1, \)0x1\([^\n]*\n[^\n]*\n[^\n]*setSecure[^\n]*\n\)@\10x0\2@' filename
(передайте -i
для редактирования на месте после тестирования).Это работает, сначала читая весь файл в буфер хранения, а затем сопоставляя несколько строк одновременно:
1h # If we're processing the first line, write it to the hold buffer
1!H # if not, append it to the hold buffer
$!d # If we're not at the end of the file, we're done with this line here.
x # When we finally get here, we're at the end of the file, and the whole
# file is in the hold buffer. Swap hold buffer and pattern space, so the
# whole file is in the pattern space.
# Then: apply transformation. The regex is somewhat ugly because of all the \ns,
# but all that's really happening here is that we match const/4 v1, 0x1 followed
# by two lines of which the second contains "setSecure", and then replace the
# 0x1 with 0x0.
#
# To understand it, consider that \n[^\n]*\n matches newline followed by non-
# newline characters followed by another newline, which is exactly one line.
# Similarly, \n[^\n]*setSecure[^\n]*\n matches a line
s@\(const/4 v1, \)0x1\([^\n]*\n[^\n]*\n[^\n]*setSecure[^\n]*\n\)@\10x0\2@
Поскольку вы используете MacOS: MacOS по умолчанию использует BSD sed, который ограничен в количествепути.Я думаю, что в тот день у него были проблемы с \n
в его коде, поэтому вам, возможно, придется поменять их местными символами новой строки.Хотя, честно говоря, если вы собираетесь использовать sed под MacOS, было бы менее болезненно просто установить GNU sed из homebrew.