In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article analyzes "how to use awk" with you. The content is detailed and easy to understand. Friends who are interested in "how to use awk" can follow the editor's idea to read it slowly and deeply. I hope it will be helpful to you after reading. Let's learn more about "how to use awk" with the editor.
Awk print text content
Awk: Aho, Kernighan and Weinberger
The report generator looks up the text content under specific conditions and then displays it in a specific format
Format of the awk command:
# awk [option] 'script' file1 file2...
# awk [option] 'PATTERN {action}' file1 file2...
PATTERN:
Conditions described by text characters and regular expression metacharacters can be omitted
Action:
Printf specifies the format of the output item; the format must be written
Option options:
-F specifies the text separator
Awk text processing mechanism:
Awk takes out the PATTERN-compliant text line by line and follows the specified delimiter (the default is blank
The-F option allows you to specify a separator) to split, and then output each segment according to a specific format
Output of awk:
1. Print
The format of print:
Print item1,item2,....
Note:
1. Items are separated by commas, while output is separated by white space characters
2. The output item can be a string, a numeric value, a field of the current record ($1), a variable, or an expression of awk. The numeric value is converted to a string and then output.
3. The item after the print command can be omitted, which is equivalent to print $0 ($0 represents the entire line of undivided text), so if you want to output a blank line, you need to use print "".
Segment with white space to display the first and second paragraphs of the text
# awk'{print $1 and 2} 'test.txt
This is
[root@shell ~] # df-hT | sed '1d' | awk' {print "Disk name:", $1, "Mount Point:", $7, "Total Size:", $3, "Free size", $5}'
Example:
Output 3 lines
# awk 'BEGIN {print "line one\ nline two\ nline three"}'
Line one
Line two
Line three
The user name and its uid in the output / etc/passwd
# awk-F:'{print $1 recorder 3}'/ etc/passwd
Awk variable
1. Record variables of awk built-in variables
FS: specifies the line delimiter to be used when reading text, which defaults to a blank character; equivalent to the-F option of awk
OFS: specifies the delimiter of the output, which defaults to a blank character
[root@localhost ~] # head-n 1 / etc/passwd | awk-F:'{print $1 recording 7}'
Root / bin/bash
[root@localhost ~] #
[root@localhost ~] # head-n 1 / etc/passwd | awk 'BEGIN {FS= ":"} {print $1 recording 7}'
Root / bin/bash
[root@localhost ~] # head-n 1 / etc/passwd | awk-F:'{print $1 recording 7}'
Root / bin/bash
[root@localhost ~] # head-n 1 / etc/passwd | awk-F: 'BEGIN {OFS= "- -"} {print $1 "7}'
Root---/bin/bash
[root@localhost ~] #
RS: specifies the newline character to be used when reading text (specify what character to use as the newline character); the default is newline character
ORS: specifies the line delimiter to use when outputting
2. Data variables of awk built-in variables
NR: record the number of lines of text processed by awk. If there are multiple files, all files will be counted together.
[root@localhost ~] # awk'{print ", NR," Line content: ", $0}'/ etc/hosts / etc/issue
Line 1 content: 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
Line 2: 1 localhost localhost.localdomain localhost6 localhost6.localdomain6
Line 3: CentOS release 6.6 (Final)
Line 4: Kernel\ r on an\ m
Line 5:
Note:
Print does not need to use $when displaying variable values
FNR: record the number of lines of files being processed by awk. If there are multiple files, each file is counted separately.
[root@localhost ~] # awk'{print ", FNR," Line content: ", $0}'/ etc/hosts / etc/issue
Line 1 content: 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
Line 2: 1 localhost localhost.localdomain localhost6 localhost6.localdomain6
Line 1 content: CentOS release 6.6 (Final)
Line 2: Kernel\ r on an\ m
Line 3:
NF: record the current line being processed by awk is separated into several fields
# cat test.txt
This is a test.
# awk'{print NF} 'test.txt
four
# awk'{print $NF} 'test.txt
Test.
[root@localhost ~] # awk-F:'{print "Number of line:", NF}'/ etc/passwd
[root@shell] # awk-F. '{print "Number of Line:", NF}' / etc/hosts
[root@shell ~] # awk 'BEGIN {FS= "."} {print "Number of Line:", NF}' / etc/hosts
Number of Line: 6
Number of Line: 3
Number of Line: 6
3. User-defined variables
Awk allows users to customize variables. Variable names cannot start with a number and are case-sensitive.
Example:
Method 1: use the-v option
[root@localhost ~] # head-n 3 / etc/passwd | awk-v test= "hello"-F:'{print test, $1}'
Hello root
Hello bin
Hello daemon
Method 2: define variables in BEGIN {} mode
[root@localhost ~] # head-n 3 / etc/passwd | awk-F: 'BEGIN {test= "hello"} {print test, $1}'
Hello root
Hello bin
Hello daemon
[root@localhost ~] #
Second, printf, format the output content
Format:
Printf format,item1,item2,...
Main points:
1. Specify the format format when printf is exported
2. Format is used to specify the format of each subsequent item output
3. The printf statement does not automatically print newline characters\ n
Format:
% c: display a single character
% dline% I: decimal integer
% eForce% E: display value by scientific counting method
% f: show floating point number
% gpeng% G: displays values in scientific counting format or floating point format
% s: display string
% u: unsigned integer
%%: show% itself
Modifier:
N: display width, N is a number
-: left alignment, default is right alignment
+: display numeric symbol
Example:
[root@localhost ~] # head-n 3 / etc/passwd | awk-F:'{printf "%-10s%-8s%-20s\ n", $1
Root / root / bin/bash
Bin/ bin/ sbin/nologin
Daemon / sbin/ sbin/nologin
[root@shell ~] # df-hT | sed '1d' | awk' {printf "%-15s%-10s\ n", $1m 7}'
/ dev/vda2 /
Tmpfs / dev/shm
/ dev/vda1 / boot
[root@shell ~] #
[root@shell ~] # awk-F:'{printf "%-15s%-10d%-25s%-15s\ n", $1 for 3 15s%-10d%-25s%-15s, 6 for 7}'/ etc/passwd
Root 0 / root / bin/bash
Bin 1 / bin/ sbin/nologin
Daemon 2 / sbin/ sbin/nologin
Adm 3 / var/adm / sbin/nologin
Lp 4 / var/spool/lpd / sbin/nologin
[root@node2 ~] # df-hT | sed '1d' | awk' {printf "%-10s%-10s%-12s%-10s%-15s%-6s%-8s%-4s\ n", "DiskName:", $1, "MountPoint:", $7, "TotalSize:", $3, "Usage:", $6}'
DiskName: / dev/sda3 MountPoint: / TotalSize: 77g Usage: 6
DiskName: tmpfs MountPoint: / dev/shm TotalSize: 491m Usage: 0
DiskName: / dev/sda1 MountPoint: / boot TotalSize: 190m Usage: 15
# awk [option] 'PATTERN {action}' file1 file2...
Action:
Printf
Options:
-F
-v
PATTERN representation:
1. Regular expression with the format of / regex/
Display the first paragraph of the line starting with r in / etc/passwd with a colon as the delimiter
# awk-F:'/ ^ r / {print $1}'/ etc/passwd
Root
Rpc
Rtkit
Rpcuser
[root@localhost ~] # awk'/ ^ (r | s) / {print $0}'/ etc/passwd
[root@localhost ~] # awk'/ ^ [rs] / {print $0}'/ etc/passwd
[root@localhost ~] # ls-l / tmp/ | awk'/ ^ d / {print $9}'
[root@localhost ~] # netstat-antp | awk'/ ^ tcp/ {print $0}'
[root@localhost ~] # netstat-antp | sed '1mai 2d'
2. Expression, an expression consisting of the following operators
Operators for awk
1. Arithmetic operator
-x negative value
+ x is converted to numeric value, positive value
X ^ y, x ^ 3 to the power of x ^ y
Xfanty
X/y
Xfanty
XMury
X% y
2. String operator
+: implement string concatenation "ab" + "cd" abcd "ab" + "12" ab12
3. Assignment operator
=
+ = axiomb a=a+b a+=2a=a+2
-=
* =
/ =
% =
^ = x ^ = y x ^ y
* =
+ + x++x=x+1
--
4. Comparison operator
X
< y x y x >= y
X = y
X! = y
X ~ y x is a string, y is a pattern, true if x can be matched by a pattern, otherwise false "abc" ~ ^ a
X! ~ y
5. Logical relation character
& &
| | |
Displays user names and uid with uid greater than or equal to 500
# awk-F:'$3 > = 500 {print $1
Nfsnobody 65534
Wjc 500
Displays the user name and shell name with the default shell of / bin/bash
# awk-F:'$7 million = "/ bin/bash" {print $1 recorder 7}'/ etc/passwd
Or
# awk-F:'$7 ~ "bash$" {print $1 recording 7}'/ etc/passwd
Root / bin/bash
Mysql / bin/bash
Wjc / bin/bash
[root@node2 ~] # netstat-antp | awk'$6pm = "ESTABLISHED" {print $0}'
Tcp 0 52 192.168.87.102:22 192.168.87.1:49541 ESTABLISHED 1940/sshd
Tcp 0 0 192.168.87.102:22 192.168.87.1:49650 ESTABLISHED 2135/sshd
[root@node2 ~] #
[root@localhost ~] # df-hT | awk'+ $6 > 10 {print $1pm NF}'
/ dev/sda1 / boot
/ dev/sr0 / mnt
3. Specify a range in the format of pattern1,pattern2
Use colons as delimiters to display the user names, uid and shell of all lines in the middle between uid=0 and the last field ending with nologin
# awk-F:'$3pm / 7 ~ "nologin$" {print $1 / etc/passwd
Root 0 / bin/bash
Bin 1 / sbin/nologin
Sample formatted output
# awk-F:'$3 printf 7 ~ "nologin$" {printf "%-10s%-10s%-20s\ n" / etc/passwd
Root 0 / bin/bash
Bin 1 / sbin/nologin
4. BEGIN/END, special mode
BEGIN indicates that awk performs an operation before processing
END indicates that awk performs an operation before the end of the last line
Print the header using BEGIN
# awk-F: 'BEGIN {printf "%-10s%-10s%-20s\ n", "Username", "Uid", "Shell"} $3 million 0 7 ~ "nologin$" {printf "%-10s%-10s%-10s\ n", $1 penny 3 printf 7} / etc/passwd
Username Uid Shell
Root 0 / bin/bash
Bin 1 / sbin/nologin
Use END to print the footer
# awk-F: 'BEGIN {printf "%-10s%-10s%-20s\ n", "Username", "Uid", "Shell"} $3 million 0END OF File... 7 ~ "nologin$" {printf "%-10s%-10s%-10s\ n", $1 END {print "END OF File..."}' / etc/passwd
Username Uid Shell
Root 0 / bin/bash
Bin 1 / sbin/nologin
END OF File...
# awk [option] 'PATTERN {action}' file1 file2...
Logic control statement
1 、 if...else
Format:
If (conditional) {statement; statement} else {statement 1; statement 2}
If statement has only one statement, {} may not write
[root@localhost ~] # awk-F:'{if ($3 million / 0) {print $1, "is administrator."}'/ etc/passwd
Use the colon as the separator to determine the first field. If it is root, the user name Admin is displayed, otherwise the user name Common User is displayed.
# awk-F:'{if ($1percent = "root") print $1, "Admin"; else print $1, "Common User"}'/ etc/passwd
Root Admin
Bin Common User
Daemon Common User
Adm Common User
Lp Common User
Sync Common User
Shutdown Common User
Formatted output
# awk-F:'{if ($1mm = "root") printf "%-15s:%-15s\ n", $1, "Admin"; else printf "%-15s:%-15s\ n", $1, "Common User"}'/ etc/passwd
Root: Admin
Bin: Common User
Daemon: Common User
Adm: Common User
Lp: Common User
Sync: Common User
Shutdown: Common User
Halt: Common User
Statistics on the number of users of the system
[root@localhost] # awk-F:'{if ($3 > 0 & & $350) sum++} END {print "The number of user:", sum}'/ etc/passwd
The number of user: 16
Remove those with disk utilization greater than 10%
# df-h | sed '1d' | awk' {if (+ $5 > 10) {print $0}}'
/ dev/sda1 194M 26M 158m 15% / boot
/ dev/sr0 2.9G 2.9G 0100% / mnt
[root@localhost ~] # awk-F:'{if ($3 million) {print $1} else {print $7}}'/ etc/passwd
[root@localhost ~] # awk-F:'{if ($3 million / 0) {count++} else {ionization +}} END {print "Administrative users:", count, "number of ordinary users:", I}'/ etc/passwd
[root@localhost ~] # awk-F:'/ bash$/ | | / nologin$/ {if ($7clients = "/ bin/bash") {print +} else {print "bash users:", I, "number of nologin users:", j}'/ etc/passwd
Number of bash users: 38 nologin users: 28
2 、 while
Format:
While (condition) {statement 1; statement 2.}
Use colons as delimiters to determine that if the length of each field in each line is greater than 4, it is displayed.
# awk-F:'{iposit while (iTun4) {print $I}; iTunes +}}'/ etc/passwd
Count the words of length 5 in test.txt files
[root@localhost] # awk'{iposit 1; while (i5) {print $I}; iTunes +}} 'test.txt
3. For traversal array
Format:
For (variable definition; condition under which the loop terminates; statement that changes the condition of the loop) {statement; statement;.}
For (iTunes 1)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.