Linux

匹配 3 個模式(2 個靜態,1 個變數)並更改變數 1

  • September 1, 2022

我一輩子都想不出這個,以為我把它整理好了,但沒有

我有一個 XML 文件,其中包含如下條目

<compat-change description="Flag {@link android.content.Context#BIND_INCLUDE_CAPABILITIES} is used to pass while-in-use capabilities from client process to bound service. In targetSdkVersion R and above, if client is a TOP activity, when this flag is present, bound service gets all while-in-use capabilities; when this flag is not present, bound service gets no while-in-use capability from client." enableAfterTargetSdk="29" id="136274596" name="PROCESS_CAPABILITY_CHANGE_ID"/>

我需要匹配 id 和 name(始終是靜態值並且按相同的順序排列)但更改 Sdk 值(它是可變的)

Sdk="29" id="136274596" name="PROCESS_CAPABILITY"

我確實嘗試過

sed -i '/Sdk=\"[0-9]\+\".*id="143937733".*name="PROCESS_CAPABILITY"/ {s/Sdk=\"[0-9]\+\"/Sdk=\"0\"/1;}'

任何建議都非常感謝

適當的 XML 工具會更好地為您服務xmlstarlet,例如

xmlstarlet ed -u '
 //compat-change[@id="136274596"][starts-with(@name,"PROCESS_CAPABILITY")]/@enableAfterTargetSdk
' -v 0 file.xml

前任。

$ xmlstarlet ed -u '//compat-change[@id="136274596"][starts-with(@name,"PROCESS_CAPABILITY")]/@enableAfterTargetSdk' -v 0 file.xml
<?xml version="1.0"?>
<compat-change description="Flag {@link android.content.Context#BIND_INCLUDE_CAPABILITIES} is used to pass while-in-use capabilities from client process to bound service. In targetSdkVersion R and above, if client is a TOP activity, when this flag is present, bound service gets all while-in-use capabilities; when this flag is not present, bound service gets no while-in-use capability from client." enableAfterTargetSdk="0" id="136274596" name="PROCESS_CAPABILITY_CHANGE_ID"/>

請參閱如何過濾 XMLStarlet 中的多個屬性?

引用自:https://unix.stackexchange.com/questions/715804