[Linux] awk learning notes

Source: Internet
Author: User

[Linux] awk learning notes
Awk Study Notes

I recently added several functions of logs. However, this log is output and cannot be found by myself. So I want to write some simple monitoring scripts to check the log details,
For example, if there is no error, how many errors are reported every day. Having thought of the previous operations and maintenance and shared awk at the same time, I thought of a simple learning.

Getting started

In the simplest input, some columns use $4 to represent __$ 0 _ as the entire output column.

[root]/root/test$ps -ef|grep uwsgi|awk ‘{print $1,$5}’root Jul24root Jul24root Jul24root Jul24root Jul24root Jul24root Jul24root Jul24root Jul24root 18:49

Formatted output:

[root]/root/test$ps -ef|grep uwsgi|awk ‘{printf “%-2s and %-4s \n”,$1,$5}’root and Jul24root and Jul24root and Jul24root and Jul24root and Jul24root and Jul24root and Jul24root and Jul24root and Jul24root and 18:51
Filter and judge

Judgment symbol! =,>, <, >=, <=, =

[Root]/root/test $ ps-ef | grep uwsgi | awk '$2 = "20596" 'root 20596 20560 0 Jul24? 00:00:19 uwsgi-x uwsgi. xml # Use the header (that is, the first line) NR [root]/root/test $ ps-ef | grep uwsgi | awk '$2 = "20596" | NR = 1 {print $7}' 00: 00: 0100: 00: 19

Built-in variables:
$0-> current record (this variable stores the content of the entire row)
$1 ~ $ N-> the nth field of the current record. The fields are separated by FS.
FS-> the default delimiter for input fields is space or Tab.
NF-> the number of fields in the current record is the number of Columns
NR-> the number of records that have been read, that is, the row number. Starting from 1, if there are multiple files, this value is constantly increasing.
FNR-> current number of records. Different from NR, this value is the row number of each file.
RS-> the input record delimiter. The default Delimiter is a line break.
OFS-> delimiter of output fields, which is also a space by default
ORS-> the output record delimiter. The default value is a line break.
FILENAME-> name of the current input file

Retrieve a specific column and display the row number:

[root]/root/test$ps -ef|grep uwsgi|awk ‘$2==”20596” || NR==1 {printf “No %s, %s \n”,NR,$7}’No 1, 00:00:01No 6, 00:00:19

Specify delimiter:

[Root]/root/test $ awk 'in in {FS = ":"} {print $1, $3, $6} '/etc/passwdroot 0/rootbin 1/bindaemon 2/sbinadm 3/var/adm # can also be written as awk-F:' {print $1, $3, $6} '/etc/passwd

Multiple separators: awk-F' [;:]'

Use Regular Expressions
$cat test.log2014-07-21 20:00:53,379 - charge - INFO - 30748 - contract_no=chuangfu-MIDS-13062014-07-21 20:00:53,406 - charge - INFO - 30748 - contract_no=chuangfu-MIDS-13062014-07-21 20:00:53,431 - charge - INFO - 30748 - contract_no=chuangfu-MIDS-13062014-07-21 20:00:53,543 - charge - INFO - 30748 - contract_no=vvvgame-CCDL-13072014-07-24 16:00:34,356 - charge - INFO - 18338 - contract_no=sennheiser-CC-14052014-07-24 16:00:34,394 - charge - INFO - 18338 - contract_no=sennheiser-CC-14052014-07-24 16:04:24,431 - charge - INFO - 19081 - contract_no=sennheiser-CC-14052014-07-24 16:04:24,479 - charge - INFO - 19081 - contract_no=sennheiser-CC-14052014-07-24 16:07:20,349 - charge - INFO - 19081 - contract_no=sennheiser-CC-14052014-07-24 16:07:20,390 - charge - INFO - 19081 - contract_no=sennheiser-CC-1405[root]/Application/2.0/nirvana/logs$awk ‘$10 ~ /MIDS/ {print NR,$1,$2}’ test.log1 2014-07-21 20:00:53,3792 2014-07-21 20:00:53,4063 2014-07-21 20:00:53,431

Here ~ It is the start of the mode. If it is an inverse of the Mode !~, // Is a regular expression.
Put the results in the file and directly use redirection.

Use if else to redirect to a file group

$awk ‘{if($10 ~ /MIDS/) print > “mids.txt”;else if($6 ~ /CCDL/) print > “ccdl.txt”; else print > “cc.txt”}’ test.log
Demo case
# Calculate the log file size $ ls-l *. log | awk '{sum + = $5} END {print sum}' 102610686 # print 99 multiplication table $ seq 9 | sed 'H; g' | awk-v RS = ''' {for (I = 1; I <= NF; I ++) printf ("% dx % d = % d % s", I, NR, I * NR, I = NR?" \ N ":" \ t ")} '1x1 = 11x2 = 2 2x2 = 41x3 = 3 2x3 = 6 3x3 = 91x4 = 4 2x4 = 8 3x4 = 12 4x4 = 161x5 = 5 2x5 = 10 3x5 = 15 4x5 = 20 5x5 = 251x6 = 6 2x6 = 12 3x6 = 18 4x6 = 24 5x6 = 30 6x6 = 361x7 = 7 2x7 = 14 3x7 = 21 4x7 = 28 5x7 = 35 6x7 = 42 7x7 = 491x8 = 8 2x8 = 16 3x8 = 24 4x8 = 32 5x8 = 40 6x8 = 48 7x8 = 56 8x8 = 641x9 = 9 2x9 = 18 3x9 = 27 4x9 = 36 5x9 = 45 6x9 = 54 7x9 = 63 8x9 = 72 9x9 = 81
How to write scripts

Reference
AWK concise tutorial

This article is from the "orangleliu notebook" blog, please be sure to keep this http://blog.csdn.net/orangleliu/article/details/38357071

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.