issue221:c_c
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue221:c_c [2025/09/28 13:38] – créée d52fr | issue221:c_c [2025/09/28 15:29] (Version actuelle) – d52fr | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | We have talked about quite a few things in our CnC articles so far. However, there is one, even I struggle with sometimes, mainly because I don’t get to use it often. So as a refresher, let’s talk about grep. As an Ubuntu newbie, it will not be high on your priority list, however, it is something you will use if you ever work in an environment that is Linux-based. Even if you never do, it is a rather weird thing to master. When you use it, to untrained eyes, it will seem like magic. | + | **We have talked about quite a few things in our CnC articles so far. However, there is one, even I struggle with sometimes, mainly because I don’t get to use it often. So as a refresher, let’s talk about grep. As an Ubuntu newbie, it will not be high on your priority list, however, it is something you will use if you ever work in an environment that is Linux-based. Even if you never do, it is a rather weird thing to master. When you use it, to untrained eyes, it will seem like magic. |
Let's start with the syntax, in other words, how we use it. It is as follows: | Let's start with the syntax, in other words, how we use it. It is as follows: | ||
Ligne 8: | Ligne 8: | ||
So it’s not difficult to grasp and the man page says this about it: | So it’s not difficult to grasp and the man page says this about it: | ||
- | grep searches | + | grep searches |
- | Let’s start simple, let’s look for a “word” (pattern) in a file. | + | Nous avons abordé plusieurs points dans nos articles de C&C jusqu' |
+ | |||
+ | Commençons par la syntaxe, autrement dit, son utilisation. La voici : | ||
+ | |||
+ | grep < | ||
+ | grep < | ||
+ | commande | grep < | ||
+ | |||
+ | Ce n'est donc pas difficile à comprendre, et la page de manuel le précise : | ||
+ | grep recherche des MOTIFS dans chaque FICHIER. Les MOTIFS sont un ou plusieurs motifs séparés par des sauts de ligne, et grep affiche chaque ligne correspondant au motif. En règle générale, les MOTIFS doivent être entre guillemets lorsque grep est utilisé dans une commande shell. | ||
+ | |||
+ | |||
+ | **Let’s start simple, let’s look for a “word” (pattern) in a file. | ||
Here is my query: grep distro / | Here is my query: grep distro / | ||
Ligne 24: | Ligne 36: | ||
Broken down, run inxi, filter it by graphics and then pipe it to grep, isolating my pattern, namely resolution | Broken down, run inxi, filter it by graphics and then pipe it to grep, isolating my pattern, namely resolution | ||
- | Great, I’m sure we are all on the same page, so far. This is the very basics of finding your pattern. A lot of people will say “word” when showing you how to use grep, but you could type “reso” instead of “resolution”, | + | Great, I’m sure we are all on the same page, so far. This is the very basics of finding your pattern. A lot of people will say “word” when showing you how to use grep, but you could type “reso” instead of “resolution”, |
- | Helpful hint no. 1: You can use the case insensitive flag if you want, say, VGA as well as vga. I use it mechanically, | + | Commençons simplement : |
+ | |||
+ | Voici ma requête : grep distro / | ||
+ | |||
+ | En détail, recherchez l' | ||
+ | |||
+ | Prenons maintenant la sortie d’un fichier et envoyons-la dans grep pour la filtrer. | ||
+ | |||
+ | Voici ma requête : | ||
+ | |||
+ | inxi -G | grep resolution | ||
+ | |||
+ | En détail, lancez inxi, filtrez par « graphics » (-G), puis envoyez-la vers grep pour isoler mon motif, à savoir « resolution ». | ||
+ | |||
+ | Super, je suis sûr que nous sommes tous sur la même longueur d’onde jusqu’ici. Voici les bases pour trouver votre motif. Beaucoup de gens diront « mot » lorsqu’ils vous montreront comment utiliser grep, mais vous pouvez saisir « reso » au lieu de « résolution » ; ce n’est pas forcément un « mot ». | ||
+ | |||
+ | |||
+ | **Helpful hint no. 1: You can use the case insensitive flag if you want, say, VGA as well as vga. I use it mechanically, | ||
The next most helpful way of finding things is via recursive searching. | The next most helpful way of finding things is via recursive searching. | ||
Ligne 38: | Ligne 67: | ||
So now our output is slightly different. I have the full pathname of the file, followed by a :-> (which is not a smiley in this case) and followed by the instances where they were found. | So now our output is slightly different. I have the full pathname of the file, followed by a :-> (which is not a smiley in this case) and followed by the instances where they were found. | ||
- | Note: I cannot just do this: grep " | + | Note: I cannot just do this: grep " |
- | We can clean up our output with -h option/ | + | Conseil pratique n° 1 : Vous pouvez utiliser l’option « insensible à la casse » (-i) si vous souhaitez, par exemple, utiliser VGA ou vga. Je l’utilise mécaniquement ; |
+ | |||
+ | La méthode la plus efficace pour trouver des informations est la recherche récursive. | ||
+ | |||
+ | Voici ma requête : | ||
+ | |||
+ | grep -r " | ||
+ | |||
+ | En résumé, recherchez mon motif dans un répertoire (nombre de fois où j’ai cité lists.ubuntu.com comme source). On peut également utiliser -R ici. | ||
+ | |||
+ | Notre résultat est donc légèrement différent. J’ai le chemin d’accès complet du fichier, suivi d’un : | ||
+ | |||
+ | Remarque : je ne peux pas simplement faire ceci : grep " | ||
+ | |||
+ | |||
+ | **We can clean up our output with -h option/ | ||
See what I did there? I want you to do this on your own files, to get a feeling. | See what I did there? I want you to do this on your own files, to get a feeling. | ||
Ligne 55: | Ligne 99: | ||
Versus the normal output: | Versus the normal output: | ||
- | Let’s circle back to my article query, what if I wanted to know how many times it finds my pattern in each file that it traverses? | + | Let’s circle back to my article query, what if I wanted to know how many times it finds my pattern in each file that it traverses?** |
- | Here’s my query: | + | Nous pouvons nettoyer notre sortie avec l' |
+ | Vous voyez ce que j'ai fait ? Je vous invite à le faire sur vos propres fichiers, pour vous faire une idée. | ||
+ | |||
+ | Le plus simple est d' | ||
+ | |||
+ | Bien que nous recherchions généralement uniquement des motifs, vous pouvez rechercher explicitement des mots. Cela peut être utile lorsque plusieurs parties correspondent à votre motif, mais que vous recherchez quelque chose de spécifique. J'ai un fichier contenant rgb et rgba. Si je recherchais le motif rgb, toutes les instances rgba seraient également renvoyées. | ||
+ | |||
+ | Voici ma requête : | ||
+ | |||
+ | grep -w " | ||
+ | |||
+ | En résumé, je souhaite que seul le « mot » rgb soit renvoyé de mon fichier cible. | ||
+ | |||
+ | Par rapport à la sortie normale : | ||
+ | |||
+ | Revenons au thème de cet article : que se passerait-il si je voulais savoir combien de fois mon motif est détecté dans chaque fichier parcouru ? | ||
+ | |||
+ | |||
+ | **Here’s my query: | ||
grep -R -c " | grep -R -c " | ||
Ligne 73: | Ligne 135: | ||
Broken down, recursively search for my pattern and give me the line number where you found the match. | Broken down, recursively search for my pattern and give me the line number where you found the match. | ||
- | Boom! | + | Boom! ** |
- | I’m going to leave it here for this issue, as it is getting a bit image heavy, (I don’t want Ronnie to fire me, yet) but we can talk about it more in the next issue. | + | Voici ma requête : |
+ | |||
+ | grep -R -c " | ||
+ | |||
+ | En détail, trouvez mon motif, puis indiquez-moi combien de fois il apparaît dans chaque fichier. | ||
+ | |||
+ | Notez que nous avons un chemin complet comme précédemment, | ||
+ | |||
+ | Et si nous avions plusieurs documents et que nous souhaitions savoir précisément où le motif apparaît ? | ||
+ | |||
+ | Voici ma requête : | ||
+ | |||
+ | grep -R -n " | ||
+ | |||
+ | Soit, recherchez mon motif de manière récursive et indiquez-moi le numéro de ligne où vous avez trouvé la correspondance. | ||
+ | |||
+ | Boum ! | ||
+ | |||
+ | |||
+ | **I’m going to leave it here for this issue, as it is getting a bit image heavy, (I don’t want Ronnie to fire me, yet) but we can talk about it more in the next issue. | ||
For those of you that saw the youtube video about figuring out who wrote what in the constitution, | For those of you that saw the youtube video about figuring out who wrote what in the constitution, | ||
- | Any corrections, | + | Any corrections, |
+ | |||
+ | Je vais m' | ||
+ | |||
+ | Pour ceux d' | ||
+ | |||
+ | Pour toute correction, car nous sommes tous humains, écrivez à : misc@fullcirclemagazine.org |
issue221/c_c.1759059496.txt.gz · Dernière modification : 2025/09/28 13:38 de d52fr