`grep -z -a -b -P --only-matching 'LUKS\xba\xbe'` stimmt nicht mit `LUKS\xba\xbe...` überein.

`grep -z -a -b -P --only-matching 'LUKS\xba\xbe'` stimmt nicht mit `LUKS\xba\xbe...` überein.

Ich verstehe nicht (basierend aufhttps://ubuntuforums.org/showthread.php?t=1643334):

% grep -a -b -P --only-matching 'LUKS\xba\xbe' /dev/sde  ### does not match
% echo -e 'LUKS\xba\xbe...' | grep -a -b -P --only-matching 'LUKS' ### matches
0:LUKS
% echo -e 'LUKS\xba\xbe...' | grep -z -a -b -P --only-matching 'LUKS' ### matches
0:LUKS
% echo -e 'LUKS\xba\xbe...' | grep -z -a -b -P --only-matching 'LUKS\xba' ### does not match
% echo -e 'LUKS\xba\xbe...' | grep -z -a -b -P --only-matching 'LUKS\xba\xbe' ### does not match

Warum liefert grep keine Übereinstimmung (obwohl es offensichtlich sollte) und was muss ich tun, damit eine Übereinstimmung erzielt wird?

Ich habe diese Befehle in Tails 4.18 und in openSUSE Leap 15.2 ausprobiert.

Antwort1

TL;DR: Der grepBefehl interpretiert seine Eingabe unter Verwendung des aktuellen Gebietsschemas.

Wie Sie gezeigt haben, stimmt das nicht überein:

echo -e 'LUKS\xba\xbe...' | grep -z -a -b -P --only-match 'LUKS\xba'

Darüber hinaus .stimmt nicht einmal das überein, das auf alles zutreffen sollte:

echo -e 'LUKS\xba\xbe...' | grep -z -a -b -P --only-match 'LUKS.'

Wenn wir jedoch das Gebietsschema für diese Sitzung festlegen ...

export LC_ALL=C

Und dann noch einmal versuchen, es funktioniert:

echo -e 'LUKS\xba\xbe...' | grep -z -a -b -P --only-match 'LUKS\xba'

Ihr Gebietsschema hängt von der Konfiguration Ihres Systems ab. Geben Sie ein, localeum alle Gebietsschemainformationen anzuzeigen. Auf meinem System ist das Standardgebietsschema LC_CTYPE="en_US.UTF-8", was meiner Meinung nach bedeutet, dass greperwartet wird, dass seine Eingabe UTF8 ist, und die Eingabe LUKS\xba\xbe...(Hex 4c 55 4b 53 ba be 2e 2e 2e) keine gültige UTF8-Sequenz ist. Sehen Sie LC_ALL, LC_CTYPE, LANGauf der Grep-Manpage nach und beachten Sie auch, dass dort steht: „Der Punkt . entspricht jedem einzelnen Zeichen.“ Es ist nicht angegeben, ob es sich um einen Kodierungsfehler handelt." (Hervorhebung von mir)

verwandte Informationen