Linux Shell programming awk and linuxshell programming awk

Source: Internet
Author: User

Linux Shell programming awk and linuxshell programming awk

  • Awk [-field-separator] 'commands' input-file (s)

Basic Mode

  • Awk-F ':'...

Use # to separate

  • Awk '{print $0}' a.txt

Print all

  • Awk '{print $1, $3}' a.txt

Print 1st and 3 columns

  • Awk 'in in {print "0"} {print $1} 'a.txt

Print 0 on the first line

  • Awk '{print $1} END {print "0"}' a.txt

Print the last line 0

  • Awk '$1 = "0" {print $0}' a.txt

Rows with the first column equal to 0

  • Awk '$1! = "0" {print $0} 'a.txt

Rows in the first column not equal to 0

  • Awk '$1 <= $2'

Rows in the first column smaller than or equal to the second column

  • Awk '$0 !~ /0/'a.txt

RegEx matches all rows whose columns do not contain 0.

  • Awk '$1 ~ /(12 | (34)/'a.txt

RegEx matches the first column with 12 or 34 rows.

  • Awk 'if ($1> 1 & $2 <1) {print $1} a.txt

The first column of the row where the first column is greater than 1 and the second column is smaller than 1

  • Awk 'if ($1> 1 | $2 <1) {print $2} a.txt

The second column of the First or Second column that is less than 1

  • Awk '{print nf rs nr}' a.txt

Number of columns of records, record separator, and number of read records are printed consecutively.

  • Awk 'nr = FNR {print $1} NR> FNR {print $2} 'a.txt B .txt

Print the first column of the First file and the second column of the Second file

  • Awk '{$1 = $1*2; print $1}' a.txt

Modify numeric print

  • Awk 'in in {LAST = 0} {if ($1> LAST) print $1; LAST = $1 }'

Compare and print the incremental series one by one

  • Awk '{total + = $1} END {print total}' a.txt

Statistical column Value

  • Awk '{printf "% c", $1}' a.txt

Format output

  • Awk '{print match ($1, "1")}' a.txt

Print the position of the first 1 In the first column without printing 0

  • Awk '{gsub (/AB/, "cd", $1); print $0}' a.txt

String replacement in the first column

  • Awk '{MAP [$1] =2 2} END {for (I in MAP) {print I, MAP [I]}' a.txt

Dictionary storage and Extraction

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.