官术网_书友最值得收藏!

  • Bash Cookbook
  • Ron Brash Ganesh Naik
  • 806字
  • 2021-07-23 19:17:34

How it works...

This section is a bit of a doozy because we are leading up to another more expansive topic, which is using regexes and wildcards with strings. We introduced them, but also showed you that you can search for terms with ${SEARCH_TERM} or Packt specifically without their use—it's just more work and more statements. Can you imagine writing a specific grep statement for each term such as Packt1, Packt2, Packt3, and onwards? No fun. 

Using the Packt Publishing website as a baseline data set, we grepped our way through the directory using the grep command, targeting only our current location, our user's home directory. Grep is a powerful tool that can be used to parse the output of commands and files using patterns, regexs, and user supplied parameters. In this case, we did not expect any string to be found matching Packt because www.packtpub.com is not the same as www.Packtpub.com. Therefore, result1.txt is an empty file.

Grep and many other utilities can be case-sensitive. To use grep in a way that's case insensitive, use the  -i flag.

In the second use of grep, we used the recursive flag (-r) and found many matches. By default, grep returns the path (containing the filename) of a match, and the line the match occurred within. To find the line number, you can also use the flag (-n).

In the third example, we demonstrated that grep can be used with multiple user-supplied arguments:

$ grep -e "Packt" -e "Publishing" -r ~/www.packtpub.com/
In this recipe, we are searching using a brute-force mechanism, which means we'll find it all by using all of our strength, literally. When performing searching on large amounts of data, or even when you perform something as seemingly simple as a search on the PacktPublishing website, more advanced and targeted algorithms help you find exactly what you want more efficiently and quicker than what we are doing here!

In the fourth and fifth executed examples, we use the find command. We also pair it with pipes and the xargs command as well. By itself, find is another very powerful CLI utility that can be used to perform search functionality (and consequently, damaging things if used irresponsibly/maliciously):

$ find "${DIRECTORY}" -type f -print | xargs grep "${SEARCH_TERM}" > result4.txt

In the preceding find command, we are using -type fwhich means that we are looking for files only within ${DIRECTORY}. Then, we pipe the results into the xargs command for use with grep. Wait! What is xargs!? Xargs is a command that's commonly used in tandem with a pipe to pass newline (carriage return) data to another command. For example, if we run ls -l (with the long flag), the results are returned like this (we've added the invisible line break or \n to illustrate this):

$ ls -l
drwxr-xr-x 7 rbrash rbrash 4096 Nov 13 21:48 Desktop\n
drwxr-xr-x 2 rbrash rbrash 4096 Feb 11 2017 Documents\n
drwxr-xr-x 7 rbrash rbrash 32768 Nov 14 10:54 Downloads\n
-rw-r--r-- 1 rbrash rbrash 8980 Feb 11 2017 examples.desktop\n
...

If we piped the results directly into another command that expected an input like the following, it would break!

$ someProgram Desktop\n Documents\n Downloads\n ...

Instead, someProgram requires input values separated by a space and not new lines:

$ someProgram Desktop Documents Downloads ...

This is why you use xargs: to remove or convert the new lines into something less problematic.

Going back to the second find command example, you can see that we used the -name and ! -name parameters. -name is simple; we are looking for a file with a specific user-supplied name. In the second ! -name instance, the ! means without or not with that name. This is called inverted logic.

We also used the * wildcard again in a different context than in the first example using grep (again, more on this later). Instead, this time, we used the * to match anything before the file's extension (*.xml or *.css). It can even be used like this: 

$ ls -l /etc/*/*.txt
-rw-r--r-- 1 root root 17394 Nov 10 2015 /etc/X11/rgb.txt

In the following grep command, we use an inline subshell execution of the ls command using wildcards. Then, we take the result by setting ${RES} to $?$? is a special variable used to get the return code. Using the value within ${RES}, we can now provide a bit of conditional logic if results are found and appropriately echo:

We found results!

Right before we exited the shell, we thought we would throw in a bonus: you can search your past ran commands using the history command and grep.

主站蜘蛛池模板: 曲阜市| 西青区| 富裕县| 喀喇沁旗| 华安县| 高台县| 新竹市| 张家界市| 余江县| 保德县| 前郭尔| 老河口市| 万载县| 张北县| 台湾省| 德格县| 凤台县| 望江县| 南华县| 昭觉县| 汤原县| 吉木萨尔县| 兖州市| 张家界市| 巩留县| 荆门市| 南昌市| 通城县| 澄江县| 南京市| 卢氏县| 民县| 岑溪市| 宣恩县| 中阳县| 星子县| 龙门县| 察雅县| 章丘市| 井陉县| 温宿县|