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

  • 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.

主站蜘蛛池模板: 斗六市| 宁安市| 宁海县| 绥化市| 四平市| 泸定县| 大洼县| 封开县| 宜都市| 来安县| 定西市| 伊川县| 六枝特区| 寻乌县| 东乌珠穆沁旗| 山西省| 郁南县| 和政县| 罗甸县| 红河县| 连南| 平乡县| 盘锦市| 正定县| 宽城| 库尔勒市| 静乐县| 鹤山市| 淳化县| 丹寨县| 大悟县| 临高县| 全州县| 南丹县| 云浮市| 肥东县| 曲阳县| 浦东新区| 牡丹江市| 扶风县| 浠水县|