首页 > 科技 > gawk

gawk

2006年2月9日 16点32分 发表评论 阅读评论

gawk: pattern scanning and processing language.
There are three method using gawk command.
1. general syntax:
gawk [-F field-separator] ‘commands’ input-files
The space character is default seperator, and we could identify other character such as “:”. For example,
# gawk -F: ‘commands’ input-file
2.create file include gawk commands,and then issue the gawk command with this file.
# gawk -f awk-script-file input-files

gawk has two partitions:mode and action.The mode could be any if statement,complex statement and Regular expression.When we want to make the output file more readable.the key words “BEGIN” and “END” are included.The action is describled by using { }.

Field and recorder:
The lines read by gawk is seperated into fieids which following the sequence:$1,$2…$n.The comma acts as the seperator between the field numbers.”$0″ means all the fields.
In order to print sepecified field or all fields,the “print” command is needed.This command strings should be included in ().

Examples:
Phillip Cocu is DMC.
Field Field1 Field2 Field3 Field4

# cat test1
Cocu Barcelone 1972
Keane Man.Utd 1974
# gawk ‘{print $1}’ test1
Cocu
Keane
# gawk ‘{print $1,$3}’ test1
Cocu 1972
Keane 1974
# gawk ‘{print $0}’ test1
Cocu Barcelona 1972
Keane Man.Utd 1974

tee could be used with gawk to type the result and save it to an output file.
# gawk ‘{print $1,$3}’ grade.txt | tee result.out

BEGIN and END key words make the outfile of gawk more readable.
# gawk ‘BEGIN {print “this is beginning”}{print $1″t”$4}’ file
This command line prints “this is beginning ” string before the normal output which does not include “BEGIN” key word.It is similar that END is able to print meaningful messages at final.

The following item would be attentioned:
1.insuring all the gawk command be included with single quotations.
2.the quotations in the command shoule be pairs.
3.{} contains the whole commands, and in the {},if statement must be included with ( ).

The operators of gawk are listed here:
<, <=, ==, !=, >=, ~, !~
Note,~ means the output file must be matched with the identified regular expression.For example,
# gawk ‘{if($4 ~ /Brown/) print $0}’ test.txt
the regular expression is included with pairs of back slash”/”.
# gawk ‘{if($4==”48″) print $0}’ test.txt

It’s possible to use variables insteading of field number.due to this way,we could identify and change the gawk command easily.for example,
# gawk ‘{name=$1;belt=$4;if(belts ~ /Yellow/) print name “is belt” belts}’ test.txt
gawk is not able to modify the original file, it only operates on a copy.
# gawk ‘BEGIN {print “name,string”} {if($6>$5) print $1,$3} END {print “end of result”}’ test.txt
# gawk ‘{if ($1==”J Troll”)($1=”J.L.Troll”);print $1}’ test.txt

Count function:
# gawk ‘/^[^d]/ {print $9,$5}{tot+=$5} END {print “total KB:” tot}’ test.txt

To find which partition capacity is more than “56000KB”,the following command could be issued:
# df -k | gawk ‘{$4~/^[0-9]/}{if($4>TRIGGER) print $6″t”$4}’ TRIGGER=56000
#df -k
Filesystem 1024-blocks Used Free %Used Mountpoint
—————————————————————————————-
Column 1 2 3 4 5 6

分类: 科技 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
您必须在 登录 后才能发布评论.