我有這個文本檔案
tittleofthis123
<Bunlde ver=5.0>
<Packages>
<Package Type="app" FileName="Package_ARM64_beta.msix" Offset="79" Size="5791033">
<Resources>
rescode11
</Resources>
<b4:Dependencies>
depcode12
</b4:Dependencies>
</Package>
<Package Type="app" FileName="Package_x64_beta.msix" Offset="580113" Size="7195285">
<Resources>
rescode21
rescode22
</Resources>
</Package>
<Package Type="res" FileName="Package_lang-cy.msix" Offset="579" Size="15">
<Resources>
rescode31
</Resources>
</Package>
<Package Type="res" FileName="Package_lang-af.msix" Offset="5791" Size="1578">
<Resources>
rescode41
</Resources>
</Package>
</Packages>
</Bundle>
我需要輸出
tittleofthis123
<Bunlde ver=5.0>
<Packages>
<Package Type="app" FileName="Package_x64_beta.msix" Offset="580113" Size="7195285">
<Resources>
rescode21
rescode22
</Resources>
</Package>
<Package Type="res" FileName="Package_lang-af.msix" Offset="5791" Size="1578">
<Resources>
rescode41
</Resources>
</Package>
</Packages>
</Bundle>
我試過這個
pcre2grep -M -v 'ARM64.*(\n|.)*</Package>|lang-cy.*(\n|.)*</Package>' 123.txt
But off course the result isn't right, because all the package have same </Package>, so instead filtering for ARM64 only, it filter out all to the bottom Package. And I have more Package to exclude, so probably I shouldn't use -v inverse, but no idea how to retain the Title, <Bundle>, and <Packages>
tried this and this
awk '/ARM64/,/<\/Package>/ {next} {print}' 123.txt
It actually works well. But I don't understand how to make it filter more than one Package like '/ARM64/,/<\/Package>/ and /lang-cy/,/<\/Package>/. And same, I need to exclude a lot of Package, so maybe not to do the {next} thing, still have no idea how to retain Title, <Bundle>, and <Packages>
I think this is pretty close to what I need
sed -n '/<Package/{:a;N;/\n*<\/Package>/!ba; /x64/p}' 123.txt
它也很好用,但我的無能還是一樣,不知道如何加入更多的過濾器,比如x64and lang-af。同樣關于Title, <Bundle>, and <Packages>
實際上這幾乎是相同的情況,但我完全不明白答案
uj5u.com熱心網友回復:
awk '/ARM64/,/<\/Package>/ {next} {print}' 123.txt它實際上運作良好。但我不明白如何讓它過濾多個包,比如
'/ARM64/,/<\/Package>/和/lang-cy/,/<\/Package>/
由于兩個結束條件都相等,您可以只使用||(alternative) 來構建觸發條件ARM64和lang-cy以下方式的起始條件
awk '/ARM64/||/lang-cy/,/<\/Package>/ {next} {print}' 123.txt
并||再次使用以獲得另一個排除,例如洗掉lang-af您可能會做的
awk '/ARM64/||/lang-cy/||/lang-af/,/<\/Package>/ {next} {print}' 123.txt
等等。
警告:您所擁有的似乎類似于 XML,請注意 GNUAWK最適合與可以使用正則運算式描述的物體一起使用。如果你的,不能用這些來描述,就像 XML 一樣,那么你需要使用 Chomsky Type-2 裝置而不是嚴格意義上的正則運算式的工具。
uj5u.com熱心網友回復:
這可能對您有用(GNU sed):
sed '/<Package Type/{:a;N;/<\/Package>/!ba;/_x64_\|_lang-af/!d}' file
收集 和 之間的線,<Package Type如果集合包含或</Package>,則不要洗掉它。_x64__lang-af
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/449209.html
