Tip-Linux Awk

2019-06-03  本文已影响0人  cctoken

AWK 简介

Awk is abbreviated from the names of the developers – Aho, Weinberger, and Kernighan.

AWK的功能

1.AWK Operations

2.Useful for

3.Programming Construct

syntax

awk options 'selection _criteria {action }' input-file > output-file

Options

-f program-file : Reads the AWK program source from the file
program-file, instead of from the
first command line argument.
-F fs : Use fs for the input field separator

Examples

awk '/manager/ {print}' employee.txt
用于筛选出 匹配 manager的行,打印出来,其中 '' 里面被分成了 selection 和对应的action,selection即是 匹配 manager, action是 print 整个行

awk '{print $1,$4}' employee.txt
$1,$4 分别表示为该行的第一列和第四列,切记,此处列数是从1开始的,不是从0开始,且fields的默认分隔符为空格和tab,其中 $0 表示为整行

Built In Variables In Awk
内置的变量,有一下几个
NR:表明每行的行数,比如

awk '{print NR, 0}' employee.txt 即把每行都打印出来且,带上每行的行数 1 adsb ajfisdo 2 abda fjadios 3 fjdiao oajisdf NF: 表明当前行,fields的数量 awk '{printNF}' employee.txt
表明将文件中每行最后一个field打印出来

FS: FS command contains the field separator character which is used to divide fields on the input line. The default is “white space”, meaning space and tab characters. FS can be reassigned to another character (typically in BEGIN) to change the field separator.
RS: RS command stores the current record separator character. Since, by default, an input line is the input record, the default record separator character is a newline.

OFS: OFS command stores the output field separator, which separates the fields when Awk prints them. The default is a blank space. Whenever print has several parameters separated with commas, it will print the value of OFS in between each parameter.

ORS: ORS command stores the output record separator, which separates the output lines when Awk prints them. The default is a newline character. print automatically outputs the contents of ORS at the end of whatever it is given to print.

more examples

awk 'NR==3, NR==6 {print NR,$0}' employee.txt
打印出3~6行的数据,且每行带上行号

Reference

AWK command in Unix/Linux with examples

上一篇下一篇

猜你喜欢

热点阅读