Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use awk in Linux system

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about how to use awk in the Linux system. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Awk is a language for processing text files in Linux system, and it is a powerful text analysis tool. And grep, sed have three swordsmen said, the following editor and you explain in detail the Linux system three musketeers awk detailed use.

AWK is called because it takes the first characters of the three founders Alfred Aho,Peter Weinberger and Brian Kernighan's Family Name.

Syntax awk [option parameter] 'script' var=value file (s) or awk [option parameter]-f scriptfile var=value file (s) option parameter description:

-F fs or-field-separator fs specifies the input file break delimiter, and fs is a string or a regular expression, such as-F:.

-v var=value or-asign var=value assigns a user-defined variable.

-f scripfile or-file scriptfile reads the awk command from the script file.

The-mf nnn and-mr nnn sets an inherent limit on the NNN value, the-mf option limits the maximum number of blocks allocated to the nnn, and the-mr option limits the maximum number of records. These two functions are extensions to the Bell lab version of awk and are not applicable in standard awk.

-W compact or-compat,-W traditional or-traditional runs awk in compatibility mode. So gawk behaves exactly like standard awk, and all awk extensions are ignored.

-W copyleft or-copyleft,-W copyright or-copyright prints short copyright information.

-W help or-help,-W usage or-usage prints all awk options and a short description of each option.

-W lint or-lint prints warnings about structures that cannot be ported to traditional unix platforms.

-W lint-old or-lint-old prints warnings about structures that cannot be ported to traditional unix platforms.

-W posix turns on compatibility mode. However, the following restrictions are not recognized: / x, function keywords, func, escape sequences, and when fs is a space, the new line is used as a field delimiter; operators and = cannot replace and =; fflush is invalid.

-W re-interval or-re-inerval allows the use of interval regular expressions, refer to (Posix character class in grep), such as the parenthesis expression [[: alpha:]].

-W source program-text or-source program-text uses program-text as the source code and can be mixed with the-f command.

-W version or-version prints the version of the bug report information.

Basic usage log.txt text content is as follows:

2 this is a test 3 Are you like awk This's a test 10 There are orange,apple,mongo usage one:

Awk'{[pattern] action}'{filenames} # line matching statement awk''can only be used in single quotation mark instances:

# each line is divided by space or TAB 1, 4 items $awk'{print $1 in the output text 4} 'log.txt-2 a 3 like This's 10 orange,apple,mongo # formatted output $awk' {printf "%-8s%-10s\ n", $1 Log.txt-2 a 3 like This's 10 orange,apple,mongo usage II:

Awk-F #-F is equivalent to the built-in variable FS, which specifies the split character instance:

# use "," split $awk-F,'{print $1 Are you like awk This's a test 2} 'log.txt-2 this is a test 3 Are you like awk This's a test 10 There are orange apple # or use the built-in variable $awk' BEGIN {FS= ","} {print $1 $2} 'log.txt-2 this is a test 3 Are you like awk This's a test 10 There are orange apple # uses multiple delimiters. First use space segmentation, and then use "," split $awk-F'[,]'{print $1 log.txt 5} 'log.txt-2 this test 3 Are awk This's a 10 There apple usage 3:

Awk-v # set variable instance:

$awk-va=1'{print $1 Magna} 'log.txt-2 3 3 4 This's 1 10 11$ awk-va=1-vb=s' {print $1 Magna $1b} 'log.txt-2 3 2s 3 4 3s This's 1 This'ss 10 11 10s usage 4:

Awk-f {awk script} {File name} instance:

The $awk-f cal.awk log.txt operator description = + =-= * = / =% = ^ = * * = assignment?: C conditional expression | | Logic or & & Logic and ~ and! ~ match regular expression and mismatch regular expression > = relational operator space connection +-add, subtract * /% multiply, divide and remainder + -! Unary addition, subtraction and logical non-^ * exponentiation + +-increase or decrease, as prefix or suffix $fields reference in array members to filter rows whose first column is greater than 2

$awk'$1 > 2 'log.txt # Command # output 3 Are you like awk This's a test 10 There are orange,apple,mongo filter rows where the first column equals 2

$awk'$1 columns 2 {print $1 recording 3} 'log.txt # Command # output 2 is filter rows where the first column is greater than 2 and the second column is equal to' Are'

$awk'$1 > 2 & & $2records = "Are" {print $1LING 2 recording 3} 'log.txt # Command # output 3 Are you built-in variables describing the nth field of the current record $n Fields separated by FS $0 number of complete input records ARGC command line arguments ARGIND command line location of the current file (starting from 0) ARGV array of command line arguments CONVFMT numeric conversion format (default is% .6g) ENVIRON environment variable associative array ERRNO last system error description of FIELDWIDTHS field width list (separated by space bar) FILENAME current filename FNR file count Line number FS field delimiter (default is any space) IGNORECASE if true The number of fields that match a record in NF is ignored, the number of records that NR has read is the line number, the output format of OFMT numbers starting from 1 (default value is% .6g) OFS output field delimiter, the default value is the same as the input field delimiter. ORS output record delimiter (default is a newline character) the length of the string matched by the match function RS record delimiter (default is a newline character) RSTART the first position of the string matched by the match function the SUBSEP array subscript delimiter (default is / 034) $awk 'BEGIN {printf "% 4s% 4s% 4s% 4s% 4s% 4s% 4s% 4s% 4s% 4s% 4s\ n", "FILENAME", "ARGC", "FNR", "FS" "NF", "NR", "OFS", "ORS", "RS" Printf "- n"} {printf "% 4s% 4s% 4s% 4s% 4s% 4s% 4s% 4s% 4s\ n", FILENAME,ARGC,FNR,FS,NF,NR,OFS,ORS RS} 'log.txt FILENAME ARGC FNR FS NF NR OFS ORS RS-log.txt 2 1 5 1 log.txt 2 2 5 2 log.txt 2 3 3 3 log.txt 2 4 4 4$ awk-F\ 'BEGIN {printf "% 4s% 4s% 4s% 4s% 4s% 4s% 4s% 4s% 4s\ n" "FILENAME", "ARGC", "FNR", "FS", "NF", "NR", "OFS", "ORS", "RS" Printf "- n"} {printf "% 4s% 4s% 4s% 4s% 4s% 4s% 4s% 4s% 4s\ n", FILENAME,ARGC,FNR,FS,NF,NR,OFS,ORS RS} 'log.txt FILENAME ARGC FNR FS NF NR OFS ORS RS-log.txt 2 1' 1 log.txt 2 2'1 2 log.txt 2 3'2 3 log.txt 2 4' 1 4 # output sequence number NR Matches the text line number $awk'{print NR,FNR,$1,$2,$3} 'log.txt-1 1 2 this is 2 2 3 Are you 3 3 This's a test 4 4 10 There are # specifies the output separator $awk' {print $1jig2 $5} 'OFS= "$" log.txt-2$ this $test 3$ Are $awk This's $a $10$ There $use regular String matching # output the second column contains "th" and prints the second column and the fourth column $awk'$2 ~ / th/ {print $2 ^ 4} 'log.txt-this a~ to indicate the start of the pattern. / / is the mode.

# output the line $awk'/ re/ 'log.txt-3 Are you like awk 10 There are orange,apple containing "re" Mongo ignores case $awk 'BEGIN {IGNORECASE=1} / this/' log.txt-2 this is a test This's a test mode and reverses $awk' $2! ~ / th/ {print $2 $4} 'log.txt-Are like a There orange,apple,mongo $awk'! / th/ {print $2 minus 4} 'log.txt-Are like a There orange,apple Mongoawk script about awk script We need to pay attention to two keywords BEGIN and END.

BEGIN {here is the statement before execution}

END {here is the statement to be executed after all the lines have been processed.

{here are the statements to be executed when processing each line}

Suppose there is such a document (student transcript):

$cat score.txt Marry 2143 78 84 77 Jack 2321 66 78 45 Tom 2122 48 77 71 Mike 2537 87 97 95 Bob 2415 40 57 62 our awk script is as follows:

$cat cal.awk #! / bin/awk-f # before running BEGIN {math = 0 english = 0 computer = 0 printf "NAME NO. MATH ENGLISH COMPUTER TOTAL\ n "printf" -\ n "} # running {math+=$3 english+=$4 computer+=$5 printf"%-6s%-6s% 4d% 8d% 8d% 8d\ n ", $1, $2, $3 After running END {printf "-\ n" printf "TOTAL:d% 8d% 8d\ n", math, english, computer printf "AVERAGE:.2f% 8.2f% 8.2f\ n", math/NR, english/NR, computer/NR}, let's take a look at the execution result:

$awk-f cal.awk score.txt NAME NO. MATH ENGLISH COMPUTER TOTAL-Marry 2143 78 84 77 239 Jack 2321 66 78 45 189 Tom 2122 48 77 71 196 Mike 2537 87 97 95 279 Bob 2415 40 57 62 159-TOTAL: 319 393 350 AVERAGE: 63.80 78.60 70.00 the hello world programs for some other examples of AWK are:

BEGIN {print "Hello, world!"} calculates the file size

$ls-l * .txt | awk'{sum+=$5} END {print sum}'- 666581 find lines greater than 80 in length from the file:

Awk 'length > 80' log.txt print 99 multiplication table

Seq 9 | sed 'Hintosg' | awk-v RS='''{for (iSidabbi.baidu.com) albumUnip c275f6ba07e269e33d756714.htmlsword picture indexation 50) this is the end of the use of awk, the three Musketeers of Linux system. If you want to know more about awk in the past, please follow us. The above is the Linux system-related content shared by Liangxu tutorial Network for all friends. If you want to know more about Linux, remember to follow the official account "good Linux", or scan the QR code below to follow, more practical information is waiting for you! What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

The above is how to use awk in the Linux system. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report