# Grep et caractères non imprimables

**URL:** <https://www.debian-fr.org/t/grep-et-caracteres-non-imprimables/14408>\
**Category:** Programmation\
**Created:** [Février 20, 2016, 5:45 UTC](https://www.debian-fr.org/t/grep-et-caracteres-non-imprimables/14408 "2016-02-20T17:45:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ripat](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/r/bc79bd/32.png) [@ripat](https://www.debian-fr.org/u/ripat)\
**Post date:** [Février 20, 2016, 5:45 UTC](https://www.debian-fr.org/t/grep-et-caracteres-non-imprimables/14408/1 "2016-02-20T17:45:54Z")

</div>

Ça ne vous est jamais arrivé d’être persuadé de pouvoir faire quelque-chose avec une commande et de ne pas y arriver?

Habitué aux expressions régulières compatible Perl (PCRE), j’étais persuadé qu’il était possible de “greper” des caractères non imprimables comme CR LF TAB etc… Et bien je n’y arrive pas. Impossible de faire une recherche sur les valeurs HEX des caractères non plus.

Par exemple pour les LF j’ai essayé:

`grep -l '\n' fichier
grep -l "\n" fichier
grep -l '\x0D' fichier
grep -l '\x0d' fichier`

Je sais que je peux utiliser d’autres voies (sed, od etc…) mais je trouve bizarre que ce ne soit pas prévu dans GNU grep. Problème de version (GNU grep 2.5.1)? Ou je m’y prends comme un manche? ![:smt017](https://www.debian-fr.org/uploads/default/original/1X/7bbea83f89515ab7c7a738dddb86c4d4f140bd13.gif)

---

<div class="post-metadata">

**Author:** ![tellmewhy](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/43a26b/32.png) [@tellmewhy](https://www.debian-fr.org/u/tellmewhy)\
**Post date:** [Février 20, 2016, 5:45 UTC](https://www.debian-fr.org/t/grep-et-caracteres-non-imprimables/14408/2 "2016-02-20T17:45:58Z")

</div>

.

---

<div class="post-metadata">

**Author:** ![ripat](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/r/bc79bd/32.png) [@ripat](https://www.debian-fr.org/u/ripat)\
**Post date:** [Février 20, 2016, 5:45 UTC](https://www.debian-fr.org/t/grep-et-caracteres-non-imprimables/14408/3 "2016-02-20T17:45:58Z")

</div>

@ tellmewhy: oui effectivement, mais ça englobe toute la classe des “non printable”. Je voulais matcher sur un caractère particulier. Mais j’ai trouvé.

@ ripat: RTFM! man bash.

`Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped char‐
       acters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as
       follows:
              \a alert (bell)
              \b backspace
              \e an escape character
              \f form feed
              \n new line
              \r carriage return
              \t horizontal tab
              \v vertical tab
              \\ backslash
              \' single quote
              \nnn the eight-bit character whose value is the octal value nnn (one to three digits)
              \xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
              \cx a control-x character
`

Il faut utiliser une forme spéciale d’expansion de variable du shell pour prendre en compte la séquence escape:

[code]$ printf ‘hello\tworld’ | grep -l $’\t’  
(entrée standard)

$ printf “Hello\tWorld”|grep -l $’\x09’  
(entrée standard)  
[/code]

Et ça, ça marche comme attendu. Testé sur bash et ksh93. Je n’ai pas trouvé tout seul. J’ai fait appel au forum [unix.com](http://unix.com). Il y a de très grosses pointures en script sur ce site.  
[unix.com/shell-programming-s … cters.html](http://www.unix.com/shell-programming-scripting/70550-grep-non-printable-characters.html)
