In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use the awk command of the Linux system". In the daily operation, I believe that many people have doubts about how to use the awk command of the Linux system. The editor consulted all kinds of materials and sorted out a simple and easy-to-use operation method. I hope it will be helpful to answer the doubt of "how to use the awk command of the Linux system". Next, please follow the editor to study!
I. the syntax and overview of the awk command
Awk option 'mode or condition {edit instruction}' file 1 file 2...
/ / filter and output the contents of the file that meet the criteria
Awk-f script file file 1 file 2...
/ / call editing instructions from the script to filter and output the content
* the execution result of awk can be printed and displayed through the function of print. In the process of using the awk command, you can use the logical operators & & and | |
Simple mathematical operations can also be carried out, such as +, -, /,%, ^ for addition, subtraction, multiplication, division, remainder, and multiplication, respectively. **
Awk reads information from an input file or standard input, and like sed, information is read line by line. The difference is that the awk command treats a line in a text file as a record and a part (column) of a line as a field of a record. To manipulate these different fields (columns), awk borrows a method similar to a location variable in shell, using $1, $2... The order of $9 represents different columns, and $0 represents the whole row. Different fields and different fields can be separated in a specified way, and the default delimiter for awk is a space. The awk command allows you to specify the delimiter in the form of "- F delimiter".
II. Examples of awk command usage
1) output text by line
1. Output all content
[linuxmi@linux:~/ Linuxmi] $awk'{print} 'linuxmi.py [linuxmi@linux:~/ Linuxmi] $awk' {print $0} 'linuxmi.py
Equivalent to "linuxmi.py"
2. Output lines 1-3
[linuxmi@linux:~/ Linuxmi fan] $awk 'NR==1,NR==3 {print}' linuxmi.py
Including blank lines
3. The contents of lines 3 and 5
[linuxmi@linux:~/ Linuxmi fans] $awk 'NR==3 | | NR==5 {print}' linuxmi.py
4. Output the contents of all odd lines
[linuxmi@linux:~/ Linuxmi fan] $awk'(NR%2) = = 1 {print} 'linuxmi.py
5. Output the contents of all even lines
[linuxmi@linux:~/ Linuxmi fan] $awk'(NR%2) = = 0 {print} 'linuxmi.py
6. Output lines that start with "root"
[linuxmi@linux:~/ Linuxmi fans] $awk'/ ^ root/ {print}'/ etc/passwd
7. Output lines ending in "nologin"
[linuxmi@linux:~/ Linuxmi fans] $awk'/ nologin$/ {print}'/ etc/passwd
8. Count the number of rows ending in / bin/bash
[linuxmi@linux:~/ Linuxmi fans] $awk 'BEGIN {xylene 0}; /\ / bin\ / bash$/ {x fans +}; END {print x}' / etc/passwd
9. Count the number of rows ending in / bin/bash
[linuxmi@linux:~/ Linxmi fans] $grep-c "/ bin/bash$" / etc/passwd
10. Count the number of file segments separated by spaces
[linuxmi@linux:~/ Linuxmi fans] $awk 'BEGIN {RS= "}; END {print NR}' / etc/dhcp/dhclient.conf
Note: when there are more commands, use "BEGIN …... END"
2) output text by field
1. Output the third field in each line (separated by spaces)
[linuxmi@linux:~/ Linuxmi fans] $awk'{print $3} 'linuxmi.py
2. Output the first and third fields in each line (separated by spaces)
[linuxmi@linux:~/ Linuxmi fans] $awk'{print $1 recording 3} 'linuxmi.py
3) call the Shell command in double quotation marks through the pipe
1. Call the "wc-l" command to count the number of users using "bash".
[linuxmi@linux:~/ Linuxmi fans] $awk-F:'/ bash$/ {print | "wc-l"}'/ etc/ passwd [Linuxmi @ linux:~/ Linuxmi fans] $grep-c "bash$" / etc/passwd
2. Call the "w" command and count the number of online users.
[linuxmi@linux:~/ Linuxmi fans] $awk 'BEGIN {while ("w" | getline) nasty +; {print nmer 2}}'
3. Invoke the "hostname" command and output the current user name
[linuxmi@linux:~/ Linuxmi fans] $awk 'BEGIN {"hostname" | getline; print $0}'
4) use the awk command to perform simple mathematical operations
[linuxmi@linux:~/ Linuxmi fans] $awk 'BEGIN {axi9 + b = ", (a + b)}'
(a + b) = 17
[linuxmi@linux:~/ Linuxmi fans] $awk 'BEGIN {axie 100 leading breadth 78 leading print "(a-b) =", (a-b)}'
(a-b) = 22
[linuxmi@linux:~/ Linuxmi fans] $awk 'BEGIN {a / b) = "(a / b)}'
(a / b) = 16
[linuxmi@linux:~/ Linuxmi fans] $awk 'BEGIN {axim 80 / b) = ", (a / b)}'
(a / b) = 16
[linuxmi@linux:~/ Linuxmi fans] $awk 'BEGIN {axi10 / b / 9 / s / s / (a * b) = ", (a * b)}'
(a × b) = 90
As shown below:
At this point, the study on "how to use the awk command of the Linux system" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.