Linux Text Processing tool awk

Source: Internet
Author: User
Tags uuid

Very good text processing tools, especially variables and control statements, using the awesome.
Personal understanding of the general process is as follows

1. Normal output
# $0表示正行  默认是按照行分割  $1 $2#-----------------------------------------------------------------[[email protected] test]# awk  ‘{print "hello ",$2}‘ fstabhellohellohello  /etc/fstabhello  Created
2. Variables
#FS:input field seperator,  输入  在行里面里面的分割符,默认为空白字符;#OFS:output field seperator,输出  在行里面里面的分割符,默认为空白字符;#-----------------------------------------------------------------[[email protected] test]# awk -F‘ ‘  ‘{print $4}‘ fstab[[email protected] test]# awk -v FS=‘ ‘  ‘{print $4}‘ fstab[[email protected] test]# awk -v FS=‘ ‘ -v OFS=‘:‘  ‘{print $3,$4}‘ fstab   #OFS 相当于把$3,$4  替换成$3 【OFS】$4 by:anaconda
#RS:input record seperator,     输入 默认为行分割符;   #ORS:output record seperator,   输出 默认为行分割符;   (这两个一般很少用,文本从本来一行一行,换成其他格式) #-----------------------------------------------------------------[[email protected] test]# awk -v RS=‘\n‘ -v ORS=‘\n‘  ‘{print $1}‘ fstab   #默认[[email protected] test]# awk -v RS=‘ ‘ -v ORS=‘:‘ ‘{print "hello ",$2}‘ fstab   #hello  #:hello  #:hello   ......
#NF number of field  每行字段数目   $##-----------------------------------------------------------------[[email protected] test]# awk  ‘{print NF}‘ fstab#最后一个字段值#-----------------------------------------------------------------[[email protected] test]# awk  ‘{print $NF}‘ fstab
#NR:number of record, 行号; #-----------------------------------------------------------------[[email protected] test]# awk  ‘{print NR}‘ fstab#FNR:各文件分别计数;行数;#-----------------------------------------------------------------[[email protected] test]# awk  ‘{print FNR}‘ fstab fstab
#自定义变量 -v var=value 变量名区分字符大小写;#-----------------------------------------------------------------[[email protected] test]# awk -v test=‘hello world‘  ‘{print test}‘ fstabhello world[[email protected] test]# awk  ‘{test="hello world"; print  test}‘ fstabhello world
3. The printf command is used to display the style

(1) format must be given;
(2) does not wrap automatically, you need to explicitly give the line-break control, \ n
(3) in format, you need to specify a format symbol for each item that follows.
Format characters:
%c: The ASCII code that displays the characters;
%d,%i: Displays decimal integers;
%e,%e: Numerical display of scientific counting method;
%f: Displayed as floating point number;
%g,%g: Displays values in scientific notation or floating-point form;
%s: Display string;
%u: unsigned integer;
Percent: show% itself;
Modifier:
#[.#]: The width of the first digital control display; The second # indicates the precision after the decimal point;
-: Align Left
+: Display symbols for numeric values

[[email protected] test]# awk   ‘{printf "%50s\n",$1}‘ fstab                                                 #         UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac         UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c         UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b
4. Operator
算术操作符:x+y, x-y, x*y, x/y, x^y, x%y -x +x: 转换为数值;字符串操作符:没有符号的操作符,字符串连接赋值操作符:=, +=, -=, *=, /=, %=, ^=  ++, --     比较操作符:>, >=, <, <=, !=, ==模式匹配符:  ~:是否匹配      !~:是否不匹配逻辑操作符:&&   ||   !函数调用:function_name(argu1, argu2, ...)条件表达式:selector?if-true-expression:if-false-expression#-----------------------------------------------------------------[[email protected] test]# awk -F: ‘{ $7~"/bash" ? is_bash="yes":is_bash="no";age=8+10;print $1,age,$7,is_bash}‘ passwdroot 18 /bin/bash yesbin 18 /sbin/nologin no
5. Delimitation
[[email protected] test]# awk   ‘!/^UUID/{printf "%-50s\n",$1}‘ fstab   #正则[[email protected] test]# awk   ‘/^UUID/{printf "%-50s\n",$1}‘ fstab[[email protected] test]# awk ‘$3>1000{print $1,$3}‘ /etc/passwd      #表达式[[email protected] test]#  awk -F: ‘$NF~"/bash"{print $1,$3}‘ /etc/passwd[[email protected] test]#  awk -F: ‘/^sync/,/^halt/{print $1,$3}‘ /etc/passwdsync 5shutdown 6halt 7[[email protected] test]#  awk -F: ‘(NR>=6&&NR<=8){print NR,$1,$3}‘ /etc/passwd
[[email protected] test]# awk -F: ‘BEGIN{print "username    uid \n---------"}{print $1,$3}‘ /etc/passwdusername    uid---------root 0bin 1daemon 2[[email protected] test]# awk -F: ‘{print "username    uid \n---------";print $1,$3}‘ /etc/passwdusername    uid---------root 0username    uid---------bin 1username    uid
6. Control statements
#if (condition) {statments} [[email protected] test]# awk ' {if (nf>5) print $} '/etc/fstab#if (condition) { Statments} else {statements}[[email protected] test]# awk-f: ' {if ($3>=1000) printf ' Common User:%s\n ', $; else P rintf "root or Sysuser:%s\n", $ '/etc/passwd#-----------------------------------------------------------------# while (Conditon) {statments}[[email protected] test]# awk '/^uuid/{i=1;while (i<=nf) {print $i; i++}} ' Fstab#do Statement while (condition) performs first in judging [[email protected] test]# awk '/^uuid/{i=1;do {print $i; i++;} while (I&LT;=NF)} ' fstab#-----------------------------------------------------------------#for (EXPR1;EXPR2;EXPR3) statement[[email  protected] test]# awk ' {for (i=0;i<nf;i++) {print $i}} ' fstab[[email protected] test]# awk ' begin{names[' a "]=" Zander ";  names["B"]= "Marvin"; for (i in Names) {print i,names[i]}} ' fstab #只能是关联数组 names[0]= ' Zander '; This is also associative array a zanderb marvin#-----------------------------------------------------------------#switch (expression) {case VALUE1 or/regexp/: statement, Case VALUE2 or/regexp2/: statement; ...; default:stat Ement} #next to end the processing of the bank in advance and go directly to the next line; #-----------------------------------------------------------------[[email  Protected] test]# awk-f: ' {if ($3%2!=0) next; print $1,$3} '/etc/passwd
7. Array manipulation
关联数组:array[index-expression]#index in array[[email protected] test]# awk ‘BEGIN{weekdays["mon"]="Monday";weekdays["tue"]="Tuesday"; if("mon" in weekdays){print weekdays["mon"]}else{print "not exist"}}‘Monday#for(var in array) {for-body}[[email protected] test]# awk ‘BEGIN{weekdays["mon"]="Monday";weekdays["tue"]="Tuesday";for(i in weekdays) {print weekdays[i]}}‘统计   去重复[[email protected] test]# netstat -tan | awk ‘/^tcp\>/{state[$NF]++}END{for(i in state) { print i,state[i]}}‘LISTEN 2ESTABLISHED 3
8. Functions
#rand():返回0和1之间一个随机数;#-----------------------------------------------------------------[[email protected] test]# awk ‘BEGIN{print rand()}‘0.237788#length([s]):返回指定字符串的长度;#-----------------------------------------------------------------[[email protected] test]# awk ‘BEGIN{name="zander"; print length(name)}‘6#sub 替换  -----> 一次#-----------------------------------------------------------------[[email protected] test]# awk ‘BEGIN{name="zander"; print sub("a","A",name),name}‘1 zAnder#sub 替换  -----> 全部#-----------------------------------------------------------------[[email protected] test]# awk ‘BEGIN{name="zandera"; print gsub("a","A",name),name}‘2 zAnderA#split(s,a[,r]):以r为分隔符切割字符s,并将切割后的结果保存至a所表示的数组中;#------------------------------------------------------------------[[email protected] test]# netstat -tan|awk ‘/^tcp\>/{split($5,ip,":"); count[ip[1]]+=1;}END{for(i in count){print i,count[i]}}‘

Linux Text Processing tool awk

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.