| Les deux révisions précédentesRévision précédente |  | 
| issue222:c_c [2025/11/02 22:59]  –  d52fr | issue222:c_c [2025/11/03 08:11] (Version actuelle)  –  d52fr | 
|---|
 |  | 
| The command egrep is the same as grep -E as far as I know... and does not mean ‘extended grep’ as I heard one of my co-workers name it. Rather it is extended regular expressions, but in essence, he is also right, as it is an extension of the regular grep we looked at in the last issue. (My proofreader gave me carrots about this, but I left it as is, as I’d like your opinions on the matter – you know where to post ‘em: misc@fullcirclemagazine.org - I’m not starting a flame war, I’m just gathering points of view!)** | The command egrep is the same as grep -E as far as I know... and does not mean ‘extended grep’ as I heard one of my co-workers name it. Rather it is extended regular expressions, but in essence, he is also right, as it is an extension of the regular grep we looked at in the last issue. (My proofreader gave me carrots about this, but I left it as is, as I’d like your opinions on the matter – you know where to post ‘em: misc@fullcirclemagazine.org - I’m not starting a flame war, I’m just gathering points of view!)** | 
|   |  | 
|   | Nous avons déjà abordé pas mal de sujets dans nos articles de C & C. Cependant, il y en a un avec lequel j'ai parfois moi-même du mal, principalement parce que je ne l'utilise pas souvent. Alors, pour rappel, parlons de grep. Si vous débutez sous Ubuntu, ce ne sera probablement pas votre priorité ; toutefois, vous l'utiliserez si vous travaillez un jour dans un environnement Linux. Même si ce n'est jamais le cas, sa maîtrise est assez déroutante. Pour un œil non averti, son utilisation peut paraître magique. | 
|   |  | 
|   | Grep est plutôt basique, mais si vous avez consulté sa page de manuel, vous avez peut-être vu : « grep, egrep, fgrep, rgrep - afficher les lignes correspondant à des motifs ». | 
|   |  | 
|   | La commande egrep est identique à grep -E, à ma connaissance… et ne signifie pas « grep étendu », comme je l'ai entendu dire par un de mes collègues. Il s'agit plutôt d'expressions régulières étendues, mais au fond, il a raison, car c'est une extension de la commande grep classique que nous avons vue dans le numéro précédent. (Mon correcteur m'a donné des conseils à ce sujet, mais j'ai laissé tel quel, car j'aimerais avoir votre avis – vous savez où les poster : misc@fullcirclemagazine.org – je ne cherche pas la polémique, je souhaite simplement recueillir des points de vue !) | 
|   |  | 
 |  | 
| **The same is true for the other names you see in the man page. It is just an easier way to type them. It is easier to type  | **The same is true for the other names you see in the man page. It is just an easier way to type them. It is easier to type  | 
 |  | 
| If you get that, then grep really shines when you take advantage of these regular expressions. Special characters let you match wider patterns than we did before. For example, you can find lines that contain a number "[0-9]", or maybe start with a capital letter "^[A-Z]", or end with a full stop "\.$". That last one needed the “\” as an escape character, as grep incorporates things from the shell. If I needed an exclamation mark in my query, I’d need to put an escape character before it, as the “!” has a special meaning in the shell. ** | If you get that, then grep really shines when you take advantage of these regular expressions. Special characters let you match wider patterns than we did before. For example, you can find lines that contain a number "[0-9]", or maybe start with a capital letter "^[A-Z]", or end with a full stop "\.$". That last one needed the “\” as an escape character, as grep incorporates things from the shell. If I needed an exclamation mark in my query, I’d need to put an escape character before it, as the “!” has a special meaning in the shell. ** | 
|   |  | 
|   | Il en va de même pour les autres noms que vous voyez dans la page de manuel. C'est simplement une façon plus rapide de les saisir. Il est plus facile de taper : | 
|   |  | 
|   | fgrep | 
|   |  | 
|   | que : | 
|   |  | 
|   | grep -F | 
|   |  | 
|   | En tant que débutant, je vous invite à considérer egrep comme une façon d'« approfondir vos connaissances » lorsque vous lancez votre requête. Jusqu'à présent, nous avons effectué des requêtes très simples et nous allons progressivement monter en complexité, ne vous inquiétez pas ! | 
|   |  | 
|   | Cependant, je vous propose de revenir un instant en arrière. Si vous ne savez pas ce que sont les expressions régulières, grep restera très basique. Après tout, son nom signifie littéralement « expressions régulières globales » - (G)lobal (RE)gular ex(P)ressions. | 
|   |  | 
|   | Si vous comprenez cela, alors grep révèle tout son potentiel lorsque vous exploitez ces expressions régulières. Les caractères spéciaux vous permettent de cibler des motifs plus larges qu'auparavant. Par exemple, vous pouvez trouver des lignes contenant un chiffre « [0-9] », commençant par une lettre majuscule « ^[A-Z] » ou se terminant par un point « \.$ ». Ce dernier cas nécessite l’utilisation du caractère d’échappement « \ », car grep intègre des éléments du shell. Si je souhaite inclure un point d’exclamation dans ma requête, je dois le faire précéder d’un caractère d’échappement, car le « ! » a une signification particulière dans le shell. | 
|   |  | 
 |  | 
| **However, instead of having like ten escape characters in a line, it's a lot easier to enclose the entire pattern within single or double-quotes (depending on your purpose). Then we can take it up another notch with PERL regular expressions, but that is beyond the scope of a newbie introduction. I want you to understand where things come from so you don’t just copy/paste queries you find on the internet, but make your own. | **However, instead of having like ten escape characters in a line, it's a lot easier to enclose the entire pattern within single or double-quotes (depending on your purpose). Then we can take it up another notch with PERL regular expressions, but that is beyond the scope of a newbie introduction. I want you to understand where things come from so you don’t just copy/paste queries you find on the internet, but make your own. | 
 |  | 
| The other wildcard is the asterisk (*). So my query: grep "jav*" <filename> - could find “java” as well as “javascript”. The asterisk (star) means any characters. You need to use this wisely, as it can *really slow down simple searches and should be avoided in queries that contain random letter combinations, like say 10000000 UUIDs or millions of transaction numbers. ** | The other wildcard is the asterisk (*). So my query: grep "jav*" <filename> - could find “java” as well as “javascript”. The asterisk (star) means any characters. You need to use this wisely, as it can *really slow down simple searches and should be avoided in queries that contain random letter combinations, like say 10000000 UUIDs or millions of transaction numbers. ** | 
|   |  | 
|   | Cependant, au lieu d'avoir une dizaine de caractères d'échappement par ligne, il est beaucoup plus simple d'encadrer le motif entier de guillemets simples ou doubles (selon votre objectif). On peut ensuite aller plus loin avec les expressions régulières Perl, mais cela dépasse le cadre d'une introduction pour débutants. Je souhaite que vous compreniez l'origine des choses afin que vous ne vous contentiez pas de copier-coller des requêtes trouvées sur Internet, mais que vous créiez les vôtres. | 
|   |  | 
|   | Parlons donc rapidement de ces caractères « spéciaux ». | 
|   |  | 
|   | Le point (.) correspond à n'importe quel caractère. Par exemple, pour trouver les lignes contenant le texte « f.m » : « grep "f.m" <nom_de_fichier> ». Cette commande trouvera « fcm » dans notre fichier fictif, mais aussi « fhm » (mauvais magazine !) s'il était présent. Évidemment, elle ne trouvera pas de saut de ligne ; la seule exception à la règle est donc un saut de ligne. Cependant, pour trouver « f.c.m », il faudrait ajouter le caractère d'échappement, car c'est le point que je recherche spécifiquement. Donc « \. », et non simplement « . » – la différence est-elle claire ? | 
|   |  | 
|   | L'autre caractère générique est l'astérisque (*). Ainsi, ma requête : `grep "jav*" <nom_de_fichier>` pourrait trouver aussi bien « java » que « javascript ». L'astérisque représente n'importe quel caractère. Il faut l'utiliser avec précaution, car il peut considérablement ralentir les recherches simples et il est à éviter pour les requêtes contenant des combinaisons de lettres aléatoires, comme par exemple 10 000 000 d'UUID ou des millions de numéros de transaction. | 
|   |  | 
 |  | 
| **In my ramblings above, you saw me use “[0-9]”; yep, we will quickly touch on the square brackets. The 0-9 is what’s known as a “range”, but you can be specific and search for [321], for instance, and as you saw, this does not apply to only numbers, [a-z] is just as valid! We can even lump them all in: [a-zA-Z0-9] – now we are searching for alphanumerics. | **In my ramblings above, you saw me use “[0-9]”; yep, we will quickly touch on the square brackets. The 0-9 is what’s known as a “range”, but you can be specific and search for [321], for instance, and as you saw, this does not apply to only numbers, [a-z] is just as valid! We can even lump them all in: [a-zA-Z0-9] – now we are searching for alphanumerics. | 
 |  | 
| There is a rather odd one that everyone I know forgets about, and that is the “+” plus sign. If I were to say, query: egrep "is+" <filename> looking for “issue” (random I know, but it is just to illustrate a point). This will match "is", "iss", "isssss", and so forth. This differs from the question mark (?) as you want at least one match. The question mark (?) matches zero or one occurrence of the previous character. Meaning, it may be skipped. If I were looking for “fcm” and I mistakenly used the question mark in my query, like so: egrep "f?m" <filename>, the result “fm” is valid. Is the difference clear?** | There is a rather odd one that everyone I know forgets about, and that is the “+” plus sign. If I were to say, query: egrep "is+" <filename> looking for “issue” (random I know, but it is just to illustrate a point). This will match "is", "iss", "isssss", and so forth. This differs from the question mark (?) as you want at least one match. The question mark (?) matches zero or one occurrence of the previous character. Meaning, it may be skipped. If I were looking for “fcm” and I mistakenly used the question mark in my query, like so: egrep "f?m" <filename>, the result “fm” is valid. Is the difference clear?** | 
|   |  | 
|   | Dans mes explications précédentes, vous m'avez vu utiliser « [0-9] » ; en effet, nous allons rapidement aborder le sujet des crochets. Les chiffres de 0 à 9 constituent une « plage », mais vous pouvez être plus précis et rechercher par exemple [321]. Et comme vous l'avez constaté, cela ne s'applique pas uniquement aux chiffres : [a-z] est tout aussi valide ! On peut même les regrouper dans [a-zA-Z0-9] – nous recherchons alors des caractères alphanumériques. | 
|   |  | 
|   | Il existe un signe assez étrange que tout le monde semble oublier : le signe « + ». Si je faisais la requête : egrep "is+" <nom_de_fichier> pour rechercher « issue » (un exemple aléatoire, je sais, mais c'est juste pour illustrer mon propos), cela trouverait « is », « iss », « isssss », etc. Cela diffère du point d'interrogation (?) car il faut au moins une occurrence. Le point d'interrogation (?) correspond à zéro ou une occurrence du caractère précédent. Autrement dit, il peut être ignoré. Si je recherche « fcm » et que j'utilise par erreur le point d'interrogation dans ma requête, comme ceci : egrep "f?m" <nom_de_fichier>, le résultat « fm » est valide. La différence est-elle claire ? | 
|   |  | 
 |  | 
| **Now one of the things you learn early on in Linux is to “pipe” (|) one command’s output to another, but it is also the symbol used for “or” in some languages. Some double up and you may see “|| and &&” - “or” and “and”. This principle is also valid in a query; egrep "bob|alice" <filename> - now we are searching for bob or alice. (Yes, don’t ask why the cryptography lecture is now incorporated here, but it is what it is). | **Now one of the things you learn early on in Linux is to “pipe” (|) one command’s output to another, but it is also the symbol used for “or” in some languages. Some double up and you may see “|| and &&” - “or” and “and”. This principle is also valid in a query; egrep "bob|alice" <filename> - now we are searching for bob or alice. (Yes, don’t ask why the cryptography lecture is now incorporated here, but it is what it is). | 
 |  | 
| Well, that’s my time again in the magazine, so we will continue this in the next issue, keep practising!** | Well, that’s my time again in the magazine, so we will continue this in the next issue, keep practising!** | 
|   |  | 
|   | L'une des premières choses que l'on apprend sous Linux, c'est à utiliser le symbole « pipe » (|) pour rediriger la sortie d'une commande vers une autre. Ce symbole est également utilisé comme opérateur « ou » dans certains langages. On le retrouve parfois doublé : « || » et « && » signifient « ou » et « et ». Ce principe s'applique aussi aux requêtes : `egrep "bob|alice" <nom_de_fichier>` recherche ici « bob » ou « alice ». (Ne vous demandez pas pourquoi on aborde ici le sujet de la cryptographie, mais c'est ainsi.) | 
|   |  | 
|   | Si l'on répétait notre erreur précédente, `egrep "f?m|F?M" <nom_de_fichier>`, quels seraient les résultats possibles ? Vos réponses par e-mail à misc@fullcirclemagazine.org | 
|   |  | 
|   | N'oubliez pas que le symbole « | » est aussi appelé opérateur OU. Il correspond à l'expression entière avant ou après le « | », et pas seulement au caractère qui le précède. | 
|   |  | 
|   | Eh bien, c'est tout pour moi dans le magazine, nous continuerons donc cela dans le prochain numéro, continuez à vous entraîner ! | 
 |  |