In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to use the awk tool for regular expressions in Shell programming. The content is concise and easy to understand, and it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Overview of awk
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 dint awk
Typically, the command format used by awk is as follows, where single quotation marks and curly braces "{}" are used 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 instructions from the script to filter and output content
As mentioned earlier, the sed command is often used to process an entire line, while awk tends to divide a line into multiple "fields" and then process it, and by default the field delimiter is a space or the tab key. 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.
Awk-F':'{print $1 minus 3 meme 4}'/ etc/passwd
[root@localhost ~] # awk-F:'{print $1 / etc/passwdroot x 0bin x 1daemon x 2adm x 3}'/
Awk reads information from an input file or standard input, and like sed, 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. Therefore, in the above example, the awk command processes the / etc/passwd file as shown in the figure
Awk contains several special built-in variables (available directly) as follows: FS: specifies the field delimiter for each line of text, which defaults to spaces or tab stops.
NF: the number of fields in the rows currently being processed.
NR: the line number (ordinal) of the currently processed row.
$0: the entire line content of the currently processed row.
$n: the nth field (nth column) of the currently processed row.
FILENAME: the name of the file being processed.
RS: data records are separated. The default is\ n, that is, one record per behavior.
2, usage example 1) output text by line
Awk'{print} 'test.txt / / outputs everything, which is equivalent to cat test.txt
Awk'{print $0} 'test.txt / / outputs everything, which is equivalent to cat test.txt
Awk 'NR==1,NR==3 {print}' test.txt / / output line 1-3
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.