Linuxawk details and Applications

Source: Internet
Author: User

Linuxawk details and Applications

This article is from my personal blog: linux awk details and Applications

1. awk

Awk is a powerful text analysis tool that generates a data report by analyzing the text. The principle is to read the input of each line, split it according to the separator (space by default), and then perform custom calculation.

Awk '{print $1}'/etc/passwd # print the first column of all rows in the passwd file

This is the basic syntax of awk. In awk, $ n represents the number of columns, that is, $1 -- the first column, $2 --- the second column..., but $0 represents the whole row.

Next we print the data according to the specified delimiter:

Awk-F': ''{print $1} '/etc/passwd # separate each line by a colon and print the first column

Now let's use another awk method to print the output.

cat /etc/passwd | awk -F ':' '{print $1}'

Awk has some required parameters:

ARGC command line parameter count ARGV command line parameter arrangement filename awk browsed file name FNR browsed file record count FS sets input domain separator, same as-f nf domain index, that is, the current row number read by the column No. NR awk. OFS output domain separator ORS output record separator RS control record Separator

Now let's print the value of the first column, current row number, and column number.

cat /etc/passwd | awk '{print "cols:",$1," rowNumber:",NR," colNumber:",NF}'

The syntax structure of the awk is awk "BEGIN {statements} END {statements} '. The statement in the BEGIN is to initialize some variables or print some initial data, in the middle of {}, some logic operations are performed. The statements in END {} are some outputs that need to be appended after the computation is completed.

cat /etc/passwd | awk 'BEGIN{count=0} {count=count+1;print "count=",count} END{print "END",NR}'

Let's take a look at the fZ release of awk? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcrrnd2hpbgwy2df3o1_vcd4kphbyzsbjbgfzcz0 =" brush: java; "> ls-l | awk 'BEGIN {print user, time, row} {for (I = 0; I <FNR; I ++) {print $3, $7, NR} '# print the third and seventh columns cyclically based on the total number of rows, with the number of rows

ls -l | awk 'BEGIN{print "begin "} {print $0;count=0;while (count < 2) {print $9;count=count+1;} print "count=",count}'

Use awk to calculate the total file size in the current folder

ls -l | awk 'BEGIN{count=0} {count = count + $5;} END{print "total=",count}'

Awk built-in functions

Numerical calculation function: cos (x) Yu Xuan sin (x) Zheng Xuan exp (x) x's Ming log (x) x's logarithm sqrt (x) x's square int (x) returns any number n (0 <n <1). String function: sub (Ere, Repl, [In]). replace the first specific value gsub (Ere, Repl, [In]) In the string specified by the In parameter with the string specified by the Repl Parameter except that all values of the regular expression will be replaced by this, it is the same behavior as sub. index (string1, string2) returns the length ([string]) of string1 where string2 exists. returns the length of the string. If no parameter is added, the length of the entire record is returned. blength ([string]) returns the length of the string in bytes. The rest are equivalent to lengthsubstr (string, M, [N]) returns the substring starting from M and ending with N. If N is null, the substring match (string, Ere) from M to length () is returned) if the string matches the regular expression, the position where it appears is returned. Starting from 1. If no, 0 split (string, A, [Ere]) is returned. The string is divided into A arrays by separator, if the values are not separated according to the Ere regular expression, the tolower (string) toupper (string) time function is defined as mktime (yyyy mm dd hh mm ss [DST]). strftime ([format [, timestamp]) format the time into IME () to get the timestamp.
ls -l | awk '{str=$9;print "before=",str; sub(/.py*/,"",str); print "after=",str}'

Ls-l | awk '{str = $9; print str; data = substr (str, 0, index (str ,". "); print data} '# Remove the file Suffix from the substr Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.