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

Using wildcards and regexes

As we saw in the previous section, there was this new concept of recursive functions and the introduction of wildcards. This section will extend upon those same fundamental primitives to create more advanced searches using regexes and globbing.

It will also extend them with a number of built-in Bash features, and some one-liners (nifty tricks) to enhance our searches. In short:

  • A wildcard can be: *, {*.ooh,*.ahh}, /home/*/path/*.txt, [0-10], [!a], ?, [a,p] m
  • A regex can be: $, ^, *, [], [!], | (be careful to escape this)

Globbing basically refers to a far more computer-eccentric term, which can be simply described in layman terms as extended pattern matching. Wildcards are the symbols used to describe patterns, and regex is short for regular expression, which are terms used to describe the pattern that is to match a series of data.

Globbing in Bash is powerful, but likely not the best place to perform even more advanced or intricate pattern matching. In these cases, Python or another language/tool might be more appropriate.

As we can imagine, globbing and pattern matching are really useful, but they cannot be used by every utility or application. Usually, though, they can be used at the command line with utilities such as grep. For example:

$ ls -l | grep '[[:lower:]][[:digit:]]' # Notice no result
$ touch z0.test
$ touch a1.test
$ touch A2.test
$ ls -l | grep '[[:lower:]][[:digit:]]'
-rw-rw-r-- 1 rbrash rbrash 0 Nov 15 11:31 z0.test
-rw-rw-r-- 1 rbrash rbrash 0 Nov 15 11:31 a1.test

Using the ls command, which is piped into grep with a regex, we can see that after we touch three files and re-run the command that the regex allowed us to correctly filter the output for files starting with a lowercase character, which are followed by a single digit. 

If we wanted to further enhance grep (or another command), we could use any of the following:

  • [:alpha:]: Alphabetic (case-insensitive) 
  • [:lower:]: Lowercase printable characters
  • [:upper:]: Uppercase printable characters
  • [:digit:]: Numbers in decimal 0 to 9
  • [:alnum:]: Alphanumeric (all digits and alphabetic characters)
  • [:space:]: White space meaning spaces, tabs, and newlines
  • [:graph:]: Printable characters excluding spaces
  • [:print:]: Printable characters including spaces
  • [:punct:]: Punctuation (for example, a period)
  • [:cntrl:]: Control characters (non-printable characters like when a signal is generated when you use Ctrl + C)
  • [:xdigit:]: Hexadecimal characters
主站蜘蛛池模板: 呼和浩特市| 江油市| 锡林郭勒盟| 邢台县| 衡阳市| 灵寿县| 隆化县| 峨边| 西藏| 朔州市| 万安县| 获嘉县| 南康市| 灵宝市| 尉氏县| 常德市| 濮阳县| 新巴尔虎右旗| 全南县| 西城区| 楚雄市| 皋兰县| 资阳市| 平舆县| 永定县| 大荔县| 丰顺县| 新沂市| 高雄市| 株洲市| 威信县| 奉贤区| 渝北区| 怀仁县| 万载县| 苍梧县| 喀喇沁旗| 贵州省| 中卫市| 安吉县| 淮滨县|