- 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.
- Python編程自學手冊
- C語言程序設計實踐教程(第2版)
- Web應用系統開發實踐(C#)
- Python數據可視化:基于Bokeh的可視化繪圖
- Mastering phpMyAdmin 3.4 for Effective MySQL Management
- Dependency Injection in .NET Core 2.0
- jQuery從入門到精通 (軟件開發視頻大講堂)
- Python高效開發實戰:Django、Tornado、Flask、Twisted(第3版)
- Mastering Python Networking
- The DevOps 2.5 Toolkit
- Getting Started with Hazelcast(Second Edition)
- Illustrator CC平面設計實戰從入門到精通(視頻自學全彩版)
- Struts 2.x權威指南
- Appcelerator Titanium:Patterns and Best Practices
- Practical Predictive Analytics