- 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.
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 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 f, which 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.
- Building a Game with Unity and Blender
- Vue.js前端開發基礎與項目實戰
- C/C++算法從菜鳥到達人
- SQL語言從入門到精通
- Learn Programming in Python with Cody Jackson
- Amazon S3 Cookbook
- 人人都是網站分析師:從分析師的視角理解網站和解讀數據
- The Data Visualization Workshop
- 基于Swift語言的iOS App 商業實戰教程
- 零基礎入門學習Python(第2版)
- Java程序設計入門
- Mastering ROS for Robotics Programming
- Unity 2018 Shaders and Effects Cookbook
- Unity 3D/2D移動開發實戰教程
- Red Hat Enterprise Linux Troubleshooting Guide