Use characters in awk mode (*)In the following example, strings containing localhost, localnet, lines, and capable are matched.
# awk '/l*c/{print}' /etc/localhostUse awk to match strings in the file
You may also realize that (*) will try to match the longest match it may detect.
Let's take a look at this example. The regular expression t * t indicates matching strings starting with t and ending with t in the following line:
this is tecmint, where you get the best good tutorials, how to's, guides, tecmint.
When you use the/t * t/mode, you will get the following possible results:
this is tthis is tecmintthis is tecmint, where you get tthis is tecmint, where you get the best good tthis is tecmint, where you get the best good tutorials, how tthis is tecmint, where you get the best good tutorials, how tos, guides, tthis is tecmint, where you get the best good tutorials, how tos, guides, tecmint
The wildcard (*) In/t * t/enables the awk to select the last matched item:
this is tecmint, where you get the best good tutorials, how to's, guides, tecmint
Use awk in combination with the set [character (s )]Take the collection [al1] as an example. awk will match all strings containing characters a, l, or 1 in the/etc/hosts file.
# awk '/[al1]/{print}' /etc/hostsUse awk to print matching characters in the file
The next example matches the header starting with K or k, followed by a T string:
# awk '/[Kk]T/{print}' /etc/hosts Use awk to print matching characters in the file
Specify characters in a rangeCharacters that awk can understand:
- [0-9] represents a separate number
- [A-z] represents a single lowercase letter.
- [A-Z] represents a separate capital letter
- [A-zA-Z] represents a separate letter
- [A-zA-Z 0-9] represents a separate letter or number
Let's take a look at the following example:
# awk '/[0-9]/{print}' /etc/hosts Use awk to print matching numbers in the file
In the above example, all rows in the file/etc/hosts contain at least one separate number [0-9].
Use awk with metacharacters (^)In the following example, it matches all rows starting with the given pattern:
# awk '/^fe/{print}' /etc/hosts# awk '/^ff/{print}' /etc/hostsUse awk to print the row matching the pattern
Use awk with metacharacters ($)It will match all rows ending with a given pattern:
# awk '/ab$/{print}' /etc/hosts# awk '/ost$/{print}' /etc/hosts# awk '/rs$/{print}' /etc/hostsUse awk to print the string matching the pattern
Use awk with escape characters (/)It allows you to take the character after the escape character as the text, that is, to understand it as its literal meaning.
In the following example, the First Command prints all the lines in the file. In the second command, I want to match a line with $25.00, but I do not use escape characters, therefore, nothing is printed.
The third command is correct, because here an escape character is used to escape $ to identify it as '$' (rather than a metacharacter ).
# awk '//{print}' deals.txt# awk '/$25.00/{print}' deals.txt# awk '//$25.00/{print}' deals.txtUse awk with escape characters
SummaryThe above content is not all of the filtering tools used by the awk command. The above examples are the basic operations of the awk. In the following sections, I will further introduce how to use the advanced features of awk.
From: https://linux.cn/article-7586-1.html
Address: http://www.linuxprobe.com/awk-filter-string.html