Awk common usage Summary

Source: Internet
Author: User
Split usage
Echo "hello_xiao_lan" | awk '{split ($0, B, "_"); print B [3]}'

// Substr usage

Awk '{A = substr ($1, 2); print a}' file2
 
// Calculate the mean value
Awk '{sum = $1 + sum; count ++} end {print count, sum, sum/count}' aa.txt
 
Awk '{max = ($2> Max? $2: Max)} end {print max} 'test.txt
 
Group to maximize
Awk-F', ''{max [$1] = $2> MAX [$1]? $2: Max [$1]} end {for (I in max) print I, Max [I]} 't.txt
Group summation
Awk-F', ''{sum [$1] = sum [$1] + $2} end {for (I In sum) print I, sum [I]} 't.txt
 
// Input variable
 
Awk '{if ($1 = "' $ a'") Print $0} 'test.txt
 
Sub usage:
Awk-F/'{sub (/[A-Z] +. /, "", $3); print $3} 'I .txt // Replace the continuous string with null. (on the first match) changed to gsub, all of which were replaced.

// Number of rows containing a certain character
Awk 'in in {COUNT = 0} {if ($0 ~ /Hello/) Count ++} end {print count} 'test.txt

// Match and print again.
Awk-F "|" '{if ($3 ~ /CC/) Print $0} 'aa.txt
Awk-F "|" '$3 ~ /CC/{print $0} 'aa.txt
 
// Next usage. If you call next, the commands after next will not be executed.
Awk '{If (Nr = 1) {next} Print $1, $2}' Data // the data in the first row is not displayed
Awk-F "" '$1 = "i0012" {next} {print $0}' file2
// Getline usage, different from next. When Getline is called, the following command is executed and the next row of data is used.
Awk-F "" '$1 = "i0012" {Getline; print $0}' file2

Else usage
Awk-F' | ''{if ($1> 100) {print $1;} else {print" OK "} 'test1.txt


Strftime usage
Awk-F', ''{if ($2 =" 98b8e35530ab ") Print $2, $3, $4, strftime ("% Y-% m-% d % t", $5)} 'test. log | head-3

// Print the current row number and the last column
Echo "1234/1234/bb234xx/134" | awk-F/'{print NR, $ NF }'

// Specify multiple separators
Awk-F' [:/T] ''{print $1, $3} 'Test
// A row not starting with Hello
Awk '! /^ Hello/'test.txt

// While usage
Echo "1234/1234/bb234xx/134" | awk-F'/''{I = 1; while (I <NF) {print NF, $ I; I ++ }}'

// For usage
Echo "1234/1234/bb234xx/134" | awk-F'/''{for (I = 1; I <NF; I ++) {print NF, $ I }}'


Awk common usage Summary

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.