awk摘录note

声明:以下是从网上搜的一些内容,比较杂乱,主要是方便以后查阅,如果能给您带来方便那是最好不过了,如果您发现有侵权的地方,请您告诉我,我会删除此帖。

 

awk也是一门独立的编程语言
a w k语言的最基本功能是在文件或字符串中基于指定规则浏览和抽取信息。a w k抽取信息后,才能进行其他文本操作。完整的a w k脚本通常用来格式化文本文件中的信息。

$0,意即所有域

 

重要的一点是要记住实际输入文件是不可修改的,修改的只是保存在缓存里的a w k复本

设置输入域到域变量名

 

" 确保整个a w k命令用单引号括起来。
" 确保命令内所有引号成对出现。
" 确保用花括号括起动作语句,用圆括号括起条件语句。
" 可能忘记使用花括号,也许你认为没有必要,但a w k不这样认为,将按之解释语法

awk内置变量
A R G C 命令行参数个数
A R G V 命令行参数排列
E N V I R O N 支持队列中系统环境变量的使用
FILENAME a w k浏览的文件名
F N R 浏览文件的记录数
F S 设置输入域分隔符,等价于命令行- F选项
N F 浏览记录的域个数
N R 已读的记录数
O F S 输出域分隔符
O R S 输出记录分隔符
R S 控制记录分隔符

 

内置的字符串函数
awk内置字符串函数

g s u b ( r, s ) 在整个$ 0中用s替代r

g s u b ( r, s , t ) 在整个t中用s替代r

i n d e x ( s , t ) 返回s中字符串t的第一位置

l e n g t h ( s ) 返回s长度

m a t c h ( s , r ) 测试s是否包含匹配r的字符串

s p l i t ( s , a , f s ) 在f s上将s分成序列a

s p r i n t ( f m t , e x p ) 返回经f m t格式化后的e x p

s u b ( r, s ) 用$ 0中最左边最长的子串代替s

s u b s t r ( s , p ) 返回字符串s中从p开始的后缀部分

s u b s t r ( s , p , n ) 返回字符串s中从p开始长度为n的后缀部分

 

awk中使用的屏蔽序列

/ b 退格键

/ t t a b键

/ f 走纸换页

/ d d d 八进制值

/ n 新行

/ c 任意其他特殊字符,例如/ /为反斜线符号

/ r 回车键

awk ‘BEGIN {print"/n/May/tDay/n/nMay/t/104/141/171"}’

printf修饰符
- 左对齐

Wi d t h 域的步长,用0表示0步长

. p r e c 最大字符串长度,或小数点右边的位数

表9-7 awk printf格式

% c A S C I I字符

% d 整数

% e 浮点数,科学记数法

% f 浮点数,例如(1 2 3 . 4 4)

% g a w k决定使用哪种浮点数转换e或者f

% o 八进制数

% s 字符串

% x 十六进制数

 

awk -F: ‘{print $1}’ a.txt
awk -F: ‘{print $1}’ <a.txt
cat a.txt | awk -F: ‘{print $1}’

echo “aa:bb:cc” | awk -F: ‘{print $1,$3}’

awk F: ’BEGIN {print "Name Belt/n———“}{print $1”/t",$2}’ a.txt

awk ‘BEGIN {print “Name/n————”}{print $1} END {print “end-of-report”}’ grade.txt

awk ‘{if($4~/Brown/) print $0}’ grade.txt

精确匹配
awk ‘$3==“48” {print$0}’ grade.txt

awk ‘$0 !~ /Brown/’ grade.txt

awk ‘$4!=“Brown-2” {print $0}’ grade.txt

awk ‘{if($6 < $7) print $0}’ grade.txt
设置大小写 可使用[ ]符号
awk ‘/[Gg]reen/’ grade.txt
任意字符
awk ‘$1 ~ /^…a/’ grade.txt
或关系匹配
awk ‘$0 ~/(Yellow|Brown)/’ grade.txt
行首
awk ‘/^M/’ grade.txt
AND
awk ‘{if ($1"P.Bunny" && $4“Yellow”) print $0}’ grade.txt
Or
awk ‘{if ($4==“Yellow” || $4~/Brown/) print $0}’

因为awk默认是输出$0的,所以一般不需要加上:
print $0

 

要快速查看记录个数,应使用N R
awk ‘END{print NR}’ grade.txt
N F变量显示每一条读记录中有多少个域,并在E N D部分打印输入文件名。
awk ‘{print NF,NR,$0} END{print FILENAME}’ grade.txt

awk ‘{if (NR>0 && $4~/Brown/)print $0}’ grade.txt

echo $PWD | awk -F/ ’ {print $NF}’

N F的一个强大功能是将变量$ P W D的返回值传入a w k并显示其目录。这里需要指定域分隔符/
echo “/usr/local/etc/rc.sybase” | awk -F/ ‘{print $NF}’
echo $PWD | awk ‘{print $NF}’

 

awk ‘{name=$1;belts=$4;if(belts ~/Yellow/) print name" is belt "belts}’ grade.txt

awk ‘{if ($6<$7) print $0}’ grade.txt

awk ‘BEGIN{BASELINE="27"} {if ($6<BASELINE) print $0}’ grade.txt

awk ‘{if($1==“M.Tans”) {$6=$6-1};print $1,$6,$7}’ grade.txt
awk ‘{if($1==“M.Tans”) {$6=$6-1;print $1,$6,$7}}’ grade.txt

awk ‘BEGIN{print “Name/tDifference”}{if($6<$7) {$8=$7-$6;print $1,$8}}’ grade.txt
awk ‘BEGIN{print “Name/tDifference”}{if($6<$7) {diff=$7-$6;print $1,diff}}’ grade.txt

awk ‘(tot+=$6); END{print “Club student total points :” tot}’ grade.txt

awk ‘{(tot+=$6)}; END{print “Club student total points :” tot}’ grade.txt

文件长度相加
ls -l | awk ‘/^[^d]/ {print $9"/t"$5} {tot+=$5} END {print “total KB:” tot}’

 

 

awk ‘gsub(/4842/,4899){print $0}’ grade.txt
awk ‘gsub(/4842/,4899)’ grade.txt
awk ‘BEGIN {print index(“Bunny”,“ny”)}’ grade.txt
awk ‘$1==“J.Troll” {print length($1)" "$1}’ grade.txt
awk ‘BEGIN{print length(“A FEW GOOD MEN”)}’
awk ‘BEGIN{print match(“ANCD”,/d/)}
awk ’BEGIN{print match(“ANCD”,/D/)}’
awk ‘$1==“J.Lulu” {print match($1,“u”)}’ grade.txt
awk ‘BEGIN {print split(“123-456-789”,pats_array,“-”)}’
awk ‘BEGIN {print split(“123#456#789”,myarray,“#”)}’
awk ‘$1==“J.Troll” sub(/26/,“29”,$0)’ grade.txt
awk ‘$1==“L.Tansl” {print substr($1,1,3)}’ grade.txt
awk ‘$1==“L.Tansl” {print substr($1,1,99)}’ grade.txt
awk ‘{print substr($1,3)}’ grade.txt
awk ‘BEGIN{STR=“A FEW GOOD MEN”}END{print substr(STR,7)}’ grade.txt
从s h e l l中向a w k传入字符串
echo “Stand-by” | awk ‘{print length($0)}’
STR=“mydoc.txt”
echo $STR|awk ‘{print substr($STR,1,5)}’
STR=“mydoc.txt”
echo $STR|awk ‘{print substr($STR,7)}’

echo “65” | awk ‘{printf “%c/n”,$0}’
awk ‘BEGIN{printf “%c/n”,65}’
awk ‘BEGIN{printf “%f/n”,999}’

格式化输出
awk ‘{printf “%-15s %s/n”,$1,$3}’ grade.txt
awk ‘BEGIN{print “Name/t/tS.Number”}{printf “%-15s %s/n”,$1,$3}’ grade.txt

向一行a w k命令传值
awk ‘{if ($5<AGE) print $0}’ AGE=10 grade.txt

快速查看文件系统空间容量
df -k|awk ‘{if($4<TRIGGER) print $6"/t"$4}’ TRIGGER=560000
df -k|awk ‘($4~/^[0-9]/) {if($4<TRIGGER) print $6"/t"$4}’ TRIGGER=5600000
who |awk ‘{print $1" is logged on"}’
pwd | awk ‘{if ($1==derr) print $1}’ derr=$HOME

 

grade.txt:
M.Tans 5/99 48311 Green 8 40 44

J.Lulu 06/99 48317 green 9 24 26

P.Bunny 02/99 48 Yellow 12 35 28

J.Troll 07/99 4842 aBrown-3 12 26 26

L.Tansl 05/99 4712 Brown-2 12 30 28

 

awk脚本文件
a.awk
chmod 700 a.awk

#!/bin/awk -f

#all commnet lines must start with a hash ‘#’

#name:students_tots.awk

#to call:student_tot.awk grade.txt

#prints total and average of club student points

#print a header first

BEGIN{

print “Student Date Member No. Grade Age Points Max”

print “Name Joined Gained Point Available”

print “==============”

}

#let’s add the scores of points gained

(tot+=$6)

#finished proessing now let’s print the total and average point

END{

print “Club student total points :” tot

print “Average Club Student Points:” tot/NR}

 

a w k脚本
在a w k中使用F S变量
#!/bin/awk -f

#to call:passwd.awk /etc/passwd

#print out the first and seventh fields

BEGIN{

FS=“:”}

{print $1,“/t”,$7}

向a w k脚本传值
awk script_file var=value input_file

#!/bin/awk -f
#check on how many fields in a file
#name:fieldcheck.awk
#to call:fieldcheck MAX=n FS= filename
#
NF!=MAX{
print(“line” NR " does not have " MAX “fields”)}

./fieldcheck.awk MAX=7 FS=“:” passwd

 

awk
数组
awk ‘BEGIN {print split(“123#456#789”,myarray,“#”)}’

实际上m y a r r a y数组为
Myarray1=“123”
Myarray2=“456”
Myarray3=“789”

#!/bin/awk -f
#name:arraytest.awk
#prints out an array
BEGIN{
record=“123#456#789”;
split(record,myarray,“#”)}
END{for (i in myarray) {print myarray[i]}}

./arraytest.awk /dev/null

 

数组和记录
[sam@Linux_chenwy sam]$ cat grade_student.txt

Yellow#Junior

Orange#Senior

Yellor#Junior

Purple#Junior

Brown-2#Junior

White#Senior

Orange#Senior

Red#Junior

Red#Junior

Brown-2#Senior

Yellow#Senior

Red#Junior

Blue#Senior

Green#Senior

Purple#Junior

White#Junior

[sam@Linux_chenwy sam]$ cat belts.awk

#!/bin/awk -f

#name:belts.awk

#to call:belts.awk grade2.txt

#loops through the grade2.txt file and counts how many

#belts we have in (yellow,orange,red)

#also count how many adults and juniors we have

#

#start of BEGIN

#set FS and load the arrays with our values

#B E G I N部分设置F S为符号#,即域分隔符

BEGIN{FS=“#”

#Load the belt colours we are interested in only

#因为要查找Ye l l o w、O r a n g e和R e d三个级别。

#然后在脚本中手工建立数组下标对学生做同样的操作。

#注意,脚本到此只有下标或元素,并没有给数组名本身加任何注释。

belt[“Yellow”]

belt[“Orange”]

belt[“Red”]

#end of BEGIN

#load the student type

student[“Junior”]

student[“Senior”]

}

##初始化完成后, B E G I N部分结束。记住B E G I N部分并没有文件处理操作。

#loop thru array that holds the belt colours against field-1

#if we have a match,keep a running total

#现在可以处理文件了。

#首先给数组命名为c o l o r,使用循环语句测试域1级别列是否

#等于数组元素之一(Ye l l o w、O r a n g e或R e d),

#如果匹配,依照匹配元素将运行总数保存进数组。

{for (colour in belt)

{if($1==colour)

belt[colour]++}}

#loop thru array that holds the student type against

#field-2 if we have a match,keep a runing total

#同样处理数组‘ S e n i o r _ o r _ j u n i o r’,

#浏览域2时匹配操作满足,运行总数存入j u n i o r或s e n i o r的匹配数组元素。

{for (senior_or_junior in student)

{if ($2==senior_or_junior)

student[senior_or_junior]++}}

#finished processing so print out the matches..for each array

#E N D部分打印浏览结果,对每一个数组使用循环语句并打印它。

END{for (colour in belt )print "The club has “,belt[colour],colour,”Belts"

#注意在打印语句末尾有一个/符号,用来通知a w k(或相关脚本)命令持续到下一行,

#当输入一个很长的命令,并且想分行输入时可使用这种方法。

for (senior_or_junior in student) print "The club has ",/

student[senior_or_junior],senior_or_junior,"student"}

./belts.awk grade_student.txt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值