In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
1. Advantages and disadvantages of awk:
Advantages: 1.awk is used to fetch columns, and it is simple and convenient to fetch columns alone.
2.awk supports regular expressions
3.awk support variables
4.awk supports & & and | |
5.awk supports process control statements such as if and if/else conditional control
6.awk supports loops such as while, do-while, and for loops
7.awk supports commands for jump or exit, such as break, continue, next, exit, etc.
8.awk support function
Disadvantages: awk is powerful and relatively complex
Second, the command format of awk:
Awk 'condition 1 {Action 1} condition 2 {Action 2}. File.txt
Third, the usage of awk:
1. Print lines that match root
[root@localhost ~] # awk'/ root/' / etc/passwd
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
2. The role of the Mutual F option
2.1 separate with colons, print the first column of data; without the-F option, the default is a space, and colons can be customized.
[root@localhost] # awk-F:'{print $1 for 3, 5 for 7}'/ etc/passwd
Root 0 root / bin/bash
Bin 1 bin/ sbin/nologin
Daemon 2 daemon / sbin/nologin
...
2.2-F custom delimiter to separate data
[root@localhost] # df-h
Filesystem Size Used Avail Use% Mounted on
/ dev/mapper/VolGroup00-LogVol00
57G 7.2g 47G 14% /
/ dev/sda1 99m 12m 82m 13% / boot
Tmpfs 252m 0 252m 0% / dev/shm
[root@localhost ~] # df-h | grep / $| awk'{print $4}'| awk-F "%"'{print $1}'
fourteen
The 2.3cut command is also implemented
[root@localhost ~] # df-h | grep / $| awk'{print $4}'| cut-d "%"-f 1
fourteen
2.4 when printing columns, use commas to separate columns
[root@localhost ~] # awk-F:'/ root/ {print $1 $3 $5 $7}'/ etc/passwd
Root0root/bin/bash
Operator11operator/sbin/nologin
[root@localhost ~] # awk-F:'/ root/ {print $1, 3, 5, 5, 7}'/ etc/passwd
Root 0 root / bin/bash
Operator 11 operator / sbin/nologin
2.5 when printing columns, # to separate columns
[root@localhost ~] # awk-F:'/ root/ {print $1 "#" $7 "#" $3}'/ etc/passwd
Root###/bin/bash###0
Operator###/sbin/nologin###1
The usage of 3.BEGIN and END
The usage of 3.1BEGIN
[root@localhost ~] # df-h | grep / $| awk'{print $4}'| awk-F "%" 'BEGIN {print "root_usage!"} {print $1}'
Root_usage!
fourteen
3.2BEGIN and END are used together,\ nto wrap lines.
[root@localhost ~] # df-h | grep / $| awk'{print $4}'| awk-F "%" 'BEGIN {print "\ nroot_usage!"\ n = "} {print $1} END {print" = "}'
Root_usage!
=
fourteen
=
The usage of 4.FS, OFS, NR and NF
4.1FS is equivalent to-F, with columns separated by colons
[root@localhost ~] # awk 'BEGIN {FS= ":"} / root/ {print $1, 3, 5, 7, 5 / etc/passwd
Root 0 root / bin/bash
Operator 11 operator / sbin/nologin
4.2OFS changes the default delimiter to separate columns with a # sign
[root@localhost ~] # awk-F: 'BEGIN {OFS= "#} / root/ {print $1 recorder / etc/passwd
Root###0###root###/bin/bash
Operator###11###operator###/sbin/nologin
The-v option can also be implemented
[root@localhost ~] # awk-F:-v OFS= "#" / root/ {print $1 recorder 3 meme 5 meme 7}'/ etc/passwd
Root###0###root###/bin/bash
Operator###11###operator###/sbin/nologin
The usage of 4.3 semicolon
[root@localhost ~] # awk 'BEGIN {FS= ":"; OFS= "#"} / root/ {print $1 miner 7}' / etc/passwd
Root#/bin/bash
Operator#/sbin/nologin
4.4NR is used to print line numbers, and columns can be printed at custom locations.
[root@localhost ~] # awk-F:'/ root/ {print NR,$1,$7,$5,$3}'/ etc/passwd
1 root / bin/bash root 0
12 operator / sbin/nologin operator 11
[root@localhost ~] # awk'/ root/ {print NR,$0}'/ etc/passwd
1 root:x:0:0:root:/root:/bin/bash
12 operator:x:11:0:operator:/root:/sbin/nologin
4.5 print blank lines
[root@localhost ~] # awk'/ ^ $/ {print NR}'/ etc/passwd
4.6 these two commands are equivalent, but grep is more efficient
[root@localhost ~] # awk'/ root/ {print NR,$0}'/ etc/passwd
1 root:x:0:0:root:/root:/bin/bash
12 operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~] # grep-n 'root' / etc/passwd
1:root:x:0:0:root:/root:/bin/bash
12:operator:x:11:0:operator:/root:/sbin/nologin
4.7NF is used to print the number of columns. NF prints out seven columns, and $NF prints the last column.
[root@localhost ~] # awk-F:'/ root/ {print NR,NF,$NF,$0}'/ etc/passwd
1 7 / bin/bash root:x:0:0:root:/root:/bin/bash
12 7 / sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin
Comparison in 5.awk
=
= = equal to
! = not equal to
~ match
! ~ mismatch
5.1 print the first column with 37 rows or more
[root@localhost ~] # awk-F:'NR > = 37 {print NR,$1}'/ etc/passwd
37 xfs
38 gdm
39 pegasus
40 oracle
5.2 print the fourth column greater than or equal to 60
[root@localhost shell] # cat awk.txt
1 80 1 li
2 60 2 wang
3 70 3 zhang
4 10 4 zhao
[root@localhost shell] # awk'$2 > = 60 {print $4} 'awk.txt
Li
Wang
Zhang
5.3 print the fields in the first column that match root
[root@localhost ~] # awk-F:'$1pm = "root" {print $1m 3m 5m 7R 7}'/ etc/passwd
Root 0 root / bin/bash
5.4 print full-text fields that match root
[root@localhost ~] # awk-F:'$0~/root/ {print NR,$1,$7,$5,$3}'/ etc/passwd
1 root / bin/bash root 0
12 operator / sbin/nologin operator 11
5.5 print the fields in the sixth column that match root
[root@localhost] # awk-F:'$6 ~ / root/ {print $1 recorder 5 recorder 6}'/ etc/passwd
Root root / root
Operator operator / root
5.6 print the fields in lines 7 to 12
[root@localhost /] # awk-F: 'NR==7,NR==12 {print NR,$1,$7}' / etc/passwd
7 shutdown / sbin/shutdown
8 halt / sbin/halt
9 mail / sbin/nologin
10 news
11 uucp / sbin/nologin
12 operator / sbin/nologin
6.if judgment
6.1 print if the first column equals root
[root@localhost ~] # awk-F:'{if ($1mm = "root") print $0}'/ etc/passwd
Root:x:0:0:root:/root:/bin/bash
6.2 print the first column that matches the oracle
[root@localhost ~] # cat / etc/passwd | awk'{if ($1 ~ "oracle") print NR,$0}'
40 oracle:x:500:501::/home/oracle:/bin/bash
6.3 print the last column of rows that match bash
[root@localhost ~] # awk-F:'{if ($NF~/bash/) print NR,$1,$NF}'/ etc/passwd
1 root / bin/bash
31 postgres / bin/bash
35 mysql / bin/bash
40 oracle / bin/bash
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.