slu hat geschrieben: 01.02.2024 16:26:18
Code: Alles auswählen
cat test.txt
Status of Logical Device : Optimal ( Build/Verify with fix : 1 % )
Status of Logical Device : Optimal ( Build/Verify with fix : 10 % )
Status of Logical Device : Optimal ( Build/Verify with fix : 100 % )
Status of Logical Device : Optimal ( Build/Verify with fix : 2000 % )
Das Ergebnis:
Code: Alles auswählen
cat test.txt | sed '/Status of Logical Device : Optimal ( Build\/Verify with fix :/s|[0-9]\{1,3\} %|RUN %|'
Status of Logical Device : Optimal ( Build/Verify with fix : RUN % )
Status of Logical Device : Optimal ( Build/Verify with fix : RUN % )
Status of Logical Device : Optimal ( Build/Verify with fix : RUN % )
Status of Logical Device : Optimal ( Build/Verify with fix : 2RUN % )
Perfekt!
Nun ja, wie man's nimmt. Wenn da wirklich "2000 %" im Log stünden, dann würde ich das genau so sehen wollen, denn da lief dann offenbar etwas schief. Das war der Grund, warum ich das Pattern als "[0-9]\{1,3\}" (1-3-stellige Zahlen) angegeben habe und nicht als "[0-9]\+" (beliebige Zahlen ab einer Stelle).
Der Fix ist hier recht simpel, einfach das vorhergehende Leerzeichen mit in das Pattern aufnehmen:
Code: Alles auswählen
$ cat test.txt | sed '/Status of Logical Device : Optimal ( Build\/Verify with fix :/s| [0-9]\{1,3\} %| RUN %|'
Status of Logical Device : Optimal ( Build/Verify with fix : RUN % )
Status of Logical Device : Optimal ( Build/Verify with fix : RUN % )
Status of Logical Device : Optimal ( Build/Verify with fix : RUN % )
Status of Logical Device : Optimal ( Build/Verify with fix : 2000 % )
Natürlich ist letztendlich auch das unterkomplex, denn es schützt nicht vor unplausiblen dreistelligen Zahlen:
Code: Alles auswählen
$ cat test.txt
Status of Logical Device : Optimal ( Build/Verify with fix : 0 % )
Status of Logical Device : Optimal ( Build/Verify with fix : 1 % )
Status of Logical Device : Optimal ( Build/Verify with fix : 10 % )
Status of Logical Device : Optimal ( Build/Verify with fix : 100 % )
Status of Logical Device : Optimal ( Build/Verify with fix : 111 % )
Status of Logical Device : Optimal ( Build/Verify with fix : 2000 % )
Code: Alles auswählen
$ cat test.txt | sed '/Status of Logical Device : Optimal ( Build\/Verify with fix :/s| [0-9]\{1,3\} %| RUN %|'
Status of Logical Device : Optimal ( Build/Verify with fix : RUN % )
Status of Logical Device : Optimal ( Build/Verify with fix : RUN % )
Status of Logical Device : Optimal ( Build/Verify with fix : RUN % )
Status of Logical Device : Optimal ( Build/Verify with fix : RUN % )
Status of Logical Device : Optimal ( Build/Verify with fix : RUN % )
Status of Logical Device : Optimal ( Build/Verify with fix : 2000 % )
Auch das kann man abfangen (wobei die Syntax langsam hässlich wird):
Code: Alles auswählen
$ cat test.txt | sed '/Status of Logical Device : Optimal ( Build\/Verify with fix :/s# \(100\|[0-9]\{1,2\}\) %# RUN %#'
Status of Logical Device : Optimal ( Build/Verify with fix : RUN % )
Status of Logical Device : Optimal ( Build/Verify with fix : RUN % )
Status of Logical Device : Optimal ( Build/Verify with fix : RUN % )
Status of Logical Device : Optimal ( Build/Verify with fix : RUN % )
Status of Logical Device : Optimal ( Build/Verify with fix : 111 % )
Status of Logical Device : Optimal ( Build/Verify with fix : 2000 % )
("100" oder ein bis zwei Ziffern zwischen 0 und 9)
Aber irgendwann stellt sich einfach die Frage, wie viel Hirnschmalz und mentale Gesundheit man in ein Regex-Pattern stecken will.