1, regular mode, filter out users using/bin/bash in/etc/passwd
[Email protected]:~/linux/awk$awk-V fs=":" 'begin{printf "%-10s%-10s%-10s\n", "User name", "User ID", "bash"}/\/bin\/bash$/{printf "%-10s\t%-10s\t%-10s\n", $1,$3, $NF }'/etc/passwdUser name User ID bash root0/bin/Bash GHOSTWU +/bin/Bash Guest-iilhaz999/bin/bash [email protected]:~/linux/awk$grep "/bin/bash"/etc/passwdroot:x:0:0: root:/root:/bin/bashghostwu:x: +: +: GHOSTWU,,,:/home/ghostwu:/bin/bashguest-IILHAZ:X:999:999: Guest:/tmp/guest-iilhaz:/bin/bash
> Remove \bin\bash end of line from/etc/passwd
> After the qualifying rows are found, separate the line text with the ":" Sign.
> Take out the required segments, format the output
> formatting text with begin mode and printf
2, extract/etc/passwd, more than 7 letters account, use {n,m} number of matches, need to add--re-interval
[Email protected]:~/linux/awk$awk--re-interval-v fs=":" 'begin{printf "%-10s%-10s\n", "username", "id"}/^\w{7,}/{printf "%-10s\t%-10s\n", $1,$3}'/etc/passwdUser nameIDsystemd-timesync -systemd-network101systemd-resolve102systemd-bus-proxy103Messagebus106LIGHTDM108Whoopsie109DNSMASQ theKernoops theGHOSTWU +
3, row range matching mode
Format: awk '/regular 1/,/regular 2/{action} ' FILE
Matches all rows that ghostwu the first occurrence of the row, to the Zhanzhao first occurrence of the row, "Note: is the first occurrence as a matching criterion"
[Email protected]:~/linux/awk$Cat-N ghostwu.txt1Ghostwu - Mans 2Zhangsan A 3Lisi4Ghostwu - Mans 5Zhanzhao + Mans 6Peter - Mans 7Zhanzhao - Mans[email protected]:~/linux/awk$awk '/ghostwu/,/zhanzhao/{print}'ghostwu.txt GHOSTWU - MansZhangsan ALISIGHOSTWU - MansZhanzhao + Mans
Of course, the above example can also be used before the built-in variable nr, the relationship expression pattern implementation easier
[Email protected]:~/linux/awkawk'nr>=1 && nr<=5{print}' man + Mans
4, relational operators are used in conjunction with regular matches to match the network segment that satisfies the format
[Email protected]:~/linux/awk$Cathost.txt name IP address host1192.168.1.1Host2192.177.81.1Host310.0.0.5Host4192.168.3.98Host5192.168.3.98Host6192.168.9.254[email protected]:~/linux/awk$awk--re-interval'$2~/192\.168\. [0-9] {1,3}\. [0-9] {1,3}/{print}'host.txt host1192.168.1.1Host4192.168.3.98Host5192.168.3.98Host6192.168.9.254
$: The 2nd column, followed by the ~ indicates that the second column matches the result of the regular expression followed by the true, execute action, here is the print current line {print}, if added! is to take the counter
[Email protected]:~/linux/awkawk'$2!~/192\.168\.[ 0-9]{1,3}\. [0-9] {1,3}/{print}' host.txt name IP address host2 192.177. 81.1host3 10.0. 0.5
Relational operators |
Meaning |
Usage examples |
< |
Less than |
X < y |
<= |
Less than or equal |
X <= y |
== |
Equals |
x = = y |
!= |
Not equal to |
X! = y |
>= |
Greater than or equal |
X >= y |
> |
Greater than |
X > Y |
~ |
Matches the corresponding regular match is true |
x ~/Regular/ |
!~ |
True if the corresponding regular is not matched |
x!~/Regular/ |
Pattern Summary:
> Empty mode
> Relational operation mode
> Regular mode
> Row Range Mode
>begin/end mode
Linux Common basic commands: The-awk mode usage of the Three Musketeers command (2)