In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Awk tool
In Linux/UNIX system, awk is a powerful editing tool, which reads input text line by line, searches according to the specified matching pattern, formats and outputs or filters the content that meets the requirements, and can achieve quite complex text operations without interaction. It is widely used in Shell scripts to complete a variety of automatic configuration tasks.
1. Common usage of awk
The command format used by awk is as follows, with single quotation marks and curly braces "{}" to set the processing action on the data. Awk can process the target file directly or through the "- f" read script.
Awk option 'mode or condition {edit instruction}' file 1 file 2 / filter and output contents of file symbol condition awk-f script file 1 file 2 / / call edit instruction from script, filter and output content
The sed command is often used to process an entire line, while awk tends to divide a line into multiple "fields" before processing, and the field delimiter is a space or tab key by default. The result of awk execution can be printed and displayed through the function of print. In the process of using the awk command, you can use the logical operator "& &" for "and", "|" for "or", "!" It means "not"; you can also perform simple mathematical operations, such as +, -, *, /,%, ^ for addition, subtraction, multiplication, division, remainder, and multiplier, respectively.
/ etc/passwd is a very typical format file in Linux system, and the fields are separated by ":". Most of the log files in Linux system are also format files. Extracting relevant information from these files is one of the daily work of operation and maintenance. If you need to find out the user name, user ID, group ID and other columns of / etc/passwd, execute the following awk command.
[root@localhost ~] # awk-F':'{print $1 0bin 3 2adm 4} / etc/passwdroot 0 0bin 1 1daemon 2 2adm 3 4. / / awk reads information from input files or standard input. Like sed, the information is read line by line. The difference is that awk treats a line in a text file as a record and a part (column) of a line as a field (field) in a record. To manipulate these different fields, awk borrows a method similar to location variables in shell, using $1, $2, and $3 "to represent different fields in rows (records) sequentially. In addition, awk uses $0 to represent the entire line (record). Different fields are separated by specified characters. The default delimiter for awk is a space. Awk allows you to specify delimiters in the form of "- F delimiters" on the command line. Awk contains several special built-in variables (can be used directly) as follows. The built-in variable FS specifies the field separator for each line of text, which defaults to the number of fields currently processed by space or tab NF. The number of fields currently processed by NR (ordinal) $0 is the entire line content of the currently processed row. The nth field (nth column) of the current processing line FILENAME the filename of the processed RS data record delimited, defaults to\ n, that is, one record per behavior 2. Usage example
1) output text by line
[root@localhost opt] # awk'{print} 'bbb.txt / / output all content, equivalent to cat bbb.txtthis isthe woodwoodwodthe woodthis is test [root@localhost opt] # awk' {print $0} 'bbb.txt / / output all content, equivalent to cat bbb.txtthis isthe woodwoodwodthe woodthis is tes [root@localhost opt] # awk' NR==1,NR==3 {print} 'bbb.txt / / output Line 1 / 3 this isthe woodwood [root@localhost opt] # awk' (NR > = 1) & (NR)
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.