Ceci est une ancienne révision du document !
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: grep <pattern> file; grep <pattern> filename; command | grep <pattern> So it’s not difficult to grasp and the man page says this about it: grep searches for PATTERNS in each FILE. PATTERNS is one or more patterns separated by newline characters, and grep prints each line that matches a pattern. Typically PATTERNS should be quoted when grep is used in a shell command.
Let’s start simple, let’s look for a “word” (pattern) in a file. Here is my query: grep distro /home/edd/dotfile.txt Broken down, search for the pattern “distro” inside the file at this location and we can see it in action: Let’s now take the output of a file and pipe it into grep to filter it. Here is my query: inxi -G | grep 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”, it does not need to be a “word”.
Helpful hint no. 1: You can use the case insensitive flag if you want, say, VGA as well as vga. I use it mechanically, I had to reshoot these screen-shots, as they had the -i in default. (not that it made a big difference, but I’d rather not confuse newbies.) The next most helpful way of finding things is via recursive searching. Here is my query: grep -r “lists.ubuntu.com” /home/edd/Documents/FCM25/Weekly\ news/ Broken down, search for my pattern in a directory (how many times I have cited lists.ubuntu.com as a source) . We can also use -R here. 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 “lists.ubuntu.com” /home/edd/Documents/FCM25/Weekly\ news/ - as the target of my search is a folder, not a file.
We can clean up our output with -h option/flag/tack, removing the path. See what I did there? I want you to do this on your own files, to get a feeling. Easiest is to use some text files, though it will work on config files, etcetera, but I’d rather you start with files you can modify freely. Though we usually just search for patterns, you *can explicitly search for words. This can be helpful when you have multiple parts that conform to your pattern, but you want something specific. I have a file that contains rgb and rgba. If I were to seach for the pattern rgb, it would return all the rgba instances as well. Here is my query: grep -w “rgb” /home/edd/dotfile.txt Broken down, I want only the “word” rgb to be returned out of my target file. 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?
Here’s my query: grep -R -c “lists.ubuntu.com” /home/edd/Documents/FCM25/Weekly\ news/ Broken down, find my pattern, then tell me how many times it occurs inside each file. Note that we have a full path like before, but not the location of our pattern. How about if we have a few documents and we want to find out precisely where the pattern occurs? Here’s my query: grep -R -n “lists.ubuntu.com” /home/edd/Documents/FCM25/Weekly\ news/ Broken down, recursively search for my pattern and give me the line number where you found the match. 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. For those of you that saw the youtube video about figuring out who wrote what in the constitution, yes, this is inspired by that. One gentleman used a certain phrase the others did not, so searching for it, sort of identified his contributions. Though we cannot say for sure, there is certainty that he wrote certain documents. Any corrections, we are all human, to: misc@fullcirclemagazine.org