Linux Shell Programming Example Tutorial

PHP中文网
Release: 2017-06-21 14:01:29
Original
1692 people have browsed it
  • 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 , 3 columns

  • awk 'BEGIN{print "0"}{print $1}' a.txt

The first line prints 0

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

The last line prints 0

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

The row where the first column is equal to 0

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

The first column is not equal to 0

  • awk '$1<=$2'

The rows where the first column is less than or equal to the second column

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

Regular match all rows whose columns do not contain 0

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

Regular match the first column matching 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 less than 1

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

The first column is greater than 1 or the second column is less than 1 Two columns

  • awk '{print NF RS NR}' a.txt

Continuously print the number of record columns, record separator, and read Number of records

  • 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 numerical value printing

    ##awk 'BEGIN{LAST=0} {if($1>LAST) print $1; LAST=$1}'
  • Compare one by one and print the increasing sequence

    awk '{total+=$1} END {print total}' a.txt
  • Statistical column value

    awk '{printf "%c", $1}' a.txt
  • Formatted output

    awk '{print match($1, "1")}' a.txt
  • Print the position of the first 1 in the first column , no printing 0

    awk '{gsub(/ab/,"cd",$1); print $0}' a.txt
  • First column string replacement

    awk '{MAP[$1]=$2} END {for(I in MAP){print I, MAP[I]} }' a. txt
  • Dictionary storage and retrieval

The above is the detailed content of Linux Shell Programming Example Tutorial. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template