1.awk Introduction
Awk is a powerful text analysis tool. SED is often used for the processing of an entire row, whereas awk prefers to slice each row into a field (that is, a column) with a Space and TAB key for the default delimiter. Awk is suitable for small data processing.
AWK has 3 different versions: AWK, Nawk, and gawk, which are not specifically described, generally referred to as the GNU version of awk, Gawk,gawk.
2. AWK syntax format
awk ' Pattern1{action1} pattern2{action2} ... ' filename
Awk follows two single quotation marks and braces {} to process the data in the matching pattern. Awk can handle the file specified later, or it can be piped command "|" Reads the standard output from the previous command.
3. Work Flow
The awk workflow is this: reads multiple records with a ' \ n ' newline break, then divides each record into fields by the specified field delimiter, fills the field, 0theTableshownthehave aDomain, 1 represents the first field, $n represents the nth field. The default domain separator is the SPACEBAR and TAB key.
The last command, combined with awk, shows awk a simple filter output. With the last, the data of the lander can be taken out, and the result is as follows:
[[Email protected]]# last-n 4
Root pts/1 192.168.1.100 Tue Feb 11:21 still logged in
Root PTS/1 192.168.1.100 Tue Feb 10 00:46-02:28 (01:41)
Root PTS/1 192.168.1.100 Mon Feb 9 11:41-18:30 (06:48)
Dmtsai pts/1 192.168.1.100 Mon Feb 9 11:41-11:41 (00:00)
If I want to remove the IP from the account and the login, and the account and IP are separated by [tab], this will be the case:
[[Email protected]]# last-n 4 | awk ' {print $1 ' \ t " $ $} '
Root 192.168.1.100
Root 192.168.1.100
Root 192.168.1.100
Dmtsai 192.168.1.100
4. Use awk to bulk delete shared memory
First Use "ipcs–m" to view the shared memory information, and then use "Ipcrm-m shmid" to remove the shared memory. The shell script that uses awk to bulk delete shared memory is as follows:
Ipcs-m|awk '$2~/[0-9]+/{print $2} ' |
While read S
Do
Ipcrm-m $S
Done
In combination with the introduction of awk, it should not be ugly to understand scripts. awk '$2~/[0-9]+/{print $2} ' means that the second column of each line that contains only Arabic numerals is printed, that is, the shared memory identifier. In awk, the expression is surrounded by two slashes, that is,/reg/, the regular expression is preceded by a tilde (wavy) ~ that matches it, and!~ represents a mismatch. Therefore, $2~/[0-9]+/represents a shared memory ID consisting only of Arabic numerals.
Reference documents
[1http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Use awk to bulk delete shared memory under Linux