(grep) Regex para que coincida con caracteres no ASCII?


En Linux, tengo un directorio con muchos archivos. Algunos de ellos tienen caracteres no ASCII, pero todos son válidos UTF-8. Un programa tiene un error que impide que funcione con nombres de archivo no ASCII, y tengo que averiguar cuántos se ven afectados. Iba a hacer esto con find y luego hacer un grep para imprimir los caracteres no ASCII, y luego hacer un wc -l para encontrar el número. No tiene que ser grep; puedo usar cualquier expresión regular estándar de Unix , como Perl, sed, AWK, etc.

Sin embargo, ¿hay una expresión regular para 'cualquier carácter que no sea un carácter ASCII'?

Author: Peter Mortensen, 2010-01-23

8 answers

Esto coincidirá con un solo carácter no ASCII:

[^\x00-\x7F]

Este es un PCRE válido (Expresión Regular compatible con Perl ).

También puedes usar las abreviaturas POSIX :

  • [[:ascii:]] - coincide con un solo carácter ASCII
  • [^[:ascii:]] - coincide con un solo carácter no ASCII

[^[:print:]] probablemente sea suficiente para ti.**

 237
Author: Alix Axel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-11 13:02:25

No, [^\x20-\x7E] no es ASCII.

Esto es ASCII real:

 [^\x00-\x7F]

De lo contrario, recortará las nuevas líneas y otros caracteres especiales que forman parte de la tabla ASCII.

 30
Author: Peter L,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-11 13:04:02

También puede consultar esta página: Expresiones Regulares Unicode , ya que contiene algunas clases de caracteres Unicode útiles, como:

\p{Control}: an ASCII 0x00..0x1F or Latin-1 0x80..0x9F control character.
 4
Author: Rubens Farias,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-01-23 18:58:25

Realmente no necesitas una expresión regular.

printf "%s\n" *[!\ -~]*

Esto también mostrará nombres de archivos con caracteres de control en sus nombres, pero considero que es una característica.

Si no tiene ningún archivo coincidente, el glob se expandirá a nada.

 1
Author: tripleee,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-28 12:04:51

[^\x00-\x7F] y [^[:ascii:]]pierde algunos bytes de control, por lo que cadenas pueden ser la mejor opción a veces. Por ejemplo, cat test.torrent | perl -pe 's/[^[:ascii:]]+/\n/g' hará cosas extrañas a su terminal, donde as strings test.torrent se comportará.

 1
Author: user1133275,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-03 03:53:46

Puedes usar esta expresión regular:

[^\w \xC0-\xFF]

Case ask, la opción es Multilínea.

 0
Author: CypherPotato,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-23 00:17:01

Esto resultó ser muy flexible y extensible. field field = ~ s / [^\x00 - \x7F] / / g ; # por lo tanto, todos los elementos no ASCII o específicos en cuestión podrían limpiarse. Muy agradable ya sea en la selección o pre-procesamiento de elementos que eventualmente se convertirán en claves hash.

 0
Author: Don Turnblade,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-19 20:25:07

Para Validar El Cuadro De Texto Aceptar Ascii Solo use este Patrón

[\x00-\x7F]+

 0
Author: Othman Mahmoud,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-30 19:28:33