收集整理的awk用法小结

提示:其实 man awk 提供的文档也很详细,不过整理加上中文估计还是可以对初学者有一定帮助的。

awk 用法:awk ‘ pattern {action} ‘

变量名 含义
ARGC 命令行变元个数
ARGV 命令行变元数组
FILENAME 当前输入文件名
FNR 当前文件中的记录号
FS 输入域分隔符,默认为一个空格
RS 输入记录分隔符
NF 当前记录里域个数
NR 到目前为止记录数
OFS 输出域分隔符
ORS 输出记录分隔符

1、
awk ‘/101/’ file 显示文件file中包含101的匹配行。
awk ‘/101/,/105/’ file
awk ‘$1 == 5’ file
awk ‘$1 == “CT”‘ file 注意必须带双引号
awk ‘$1 * $2 >100 ‘ file
awk ‘$2 >5 && $2<=15' file 2、 awk '{print NR,NF,$1,$NF,}' file 显示文件file的当前记录号、域数和每一行的第一个和最后一个域。 awk '/101/ {print $1,$2 + 10}' file 显示文件file的匹配行的第一、二个域加10。 awk '/101/ {print $1$2}' file awk '/101/ {print $1 $2}' file 显示文件file的匹配行的第一、二个域,但显示时域中间没有分隔符。 3、 df | awk '$4>1000000 ‘ 通过管道符获得输入,如:显示第4个域满足条件的行。

4、
awk -F “|” ‘{print $1}’ file 按照新的分隔符“|”进行操作。
awk ‘BEGIN { FS=”[: \t|]” } {print $1,$2,$3}’ file 通过设置输入分隔符(FS=”[: \t|]”)修改输入分隔符。

Sep=”|”
awk -F $Sep ‘{print $1}’ file 按照环境变量Sep的值做为分隔符。
awk -F ‘[ :\t|]’ ‘{print $1}’ file 按照正则表达式的值做为分隔符,这里代表空格、:、TAB、|同时做为分隔符。
awk -F ‘[][]’ ‘{print $1}’ file 按照正则表达式的值做为分隔符,这里代表[、]

5、
awk -f awkfile file 通过文件awkfile的内容依次进行控制。
cat awkfile /101/{print “\047 Hello! \047″} –遇到匹配行以后打印 ‘ Hello! ‘.\047代表单引号。
{print $1,$2} –因为没有模式控制,打印每一行的前两个域。

6、
awk ‘$1 ~ /101/ {print $1}’ file 显示文件中第一个域匹配101的行(记录)。

7、
awk ‘BEGIN { OFS=”%”} {print $1,$2}’ file 通过设置输出分隔符(OFS=”%”)修改输出格式。

8、
awk ‘BEGIN { max=100 ;print “max=” max} BEGIN 表示在处理任意行之前进行的操作。{max=($1 >max ?$1:max); print $1,”Now max is “max}’ file 取得文件第一个域的最大值。
(表达式1?表达式2:表达式3 相当于:
if (表达式1)
表达式2
else
表达式3
awk ‘{print ($1>4 ? “high “$1: “low “$1)}’ file

9、
awk ‘$1 * $2 >100 {print $1}’ file 显示文件中第一个域匹配101的行(记录)。

10、
awk ‘{$1 == ‘Chi’ {$3 = ‘China’; print}’ file 找到匹配行后先将第3个域替换后再显示该行(记录)。
awk ‘{$7 %= 3; print $7}’ file 将第7域被3除,并将余数赋给第7域再打印。

11、
awk ‘/tom/ {wage=$2+$3; printf wage}’ file 找到匹配行后为变量wage赋值并打印该变量。

12、
awk ‘/tom/ {count++;}
END {print “tom was found “count” times”}’ file END表示在所有输入行处理完后进行处理。

13、
awk ‘gsub(/\$/,””);gsub(/,/,””); cost+=$4; END {print “The total is $” cost>”filename”}’ file gsub函数用空串替换$和,再将结果输出到filename中。
1 2 3 $1,200.00
1 2 3 $2,300.00
1 2 3 $4,000.00

awk ‘{gsub(/\$/,””);gsub(/,/,””);
if ($4>1000&&$4<2000) c1+=$4; else if ($4>2000&&$4<3000) c2+=$4; else if ($4>3000&&$4<4000) c3+=$4; else c4+=$4; } END {printf "c1=[%d];c2=[%d];c3=[%d];c4=[%d]\n",c1,c2,c3,c4}"' file 通过if和else if完成条件语句 awk '{gsub(/\$/,"");gsub(/,/,""); if ($4>3000&&$4<4000) exit; else c4+=$4; } END {printf "c1=[%d];c2=[%d];c3=[%d];c4=[%d]\n",c1,c2,c3,c4}"' file 通过exit在某条件时退出,但是仍执行END操作。 awk '{gsub(/\$/,"");gsub(/,/,""); if ($4>3000) next;
else c4+=$4; }
END {printf “c4=[%d]\n”,c4}”‘ file
通过next在某条件时跳过该行,对下一行执行操作。

14、
awk ‘{ print FILENAME,$0 }’ file1 file2 file3>fileall 把file1、file2、file3的文件内容全部写到fileall中,格式为
打印文件并前置文件名。

15、
awk ‘ $1!=previous { close(previous); previous=$1 }
{print substr($0,index($0,” “) +1)>$1}’ fileall 把合并后的文件重新分拆为3个文件。并与原文件一致。

16、
awk ‘BEGIN {“date”|getline d; print d}’ 通过管道把date的执行结果送给getline,并赋给变量d,然后打印。

17、
awk ‘BEGIN {system(“echo \”Input your name:\\c\””); getline d;print “\nYour name is”,d,”\b!\n”}’
通过getline命令交互输入name,并显示出来。
awk ‘BEGIN {FS=”:”; while(getline< "/etc/passwd" >0) { if($1~”050[0-9]_”) print $1}}’
打印/etc/passwd文件中用户名包含050x_的用户名。

18、
awk ‘{ i=1;while(i28) flag=1;
if ((j==4||j==6||j==9||j==11)&&i>30) flag=1;
if (flag==0) {printf “%02d%02d “,j,i}
}
}
}’

19、
在awk中调用系统变量必须用单引号,如果是双引号,则表示字符串
Flag=abcd
awk ‘{print ‘$Flag’}’ 结果为abcd
awk ‘{print “$Flag”}’ 结果为$Flag

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
One Response
  1. kindle says:

    从你的文章看得出你是个很好的人,呵呵

  2. minecraft says:

    Hello, I enjoy reading all of your article post.
    I like to write a little comment to support you.

  3. minecraft says:

    When I originally commented I clicked the “Notify me when new comments are added” checkbox
    and now each time a comment is added I get three emails
    with the same comment. Is there any way you can remove me
    from that service? Cheers!

  4. Thanks for sharing your thoughts about minecraft free download 2018.
    Regards

  5. Hey very nice blog!

  6. Hmm it seems like your website ate my first comment (it was super long)
    so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying your blog.
    I as well am an aspiring blog blogger but I’m still new to the whole
    thing. Do you have any suggestions for inexperienced blog
    writers? I’d really appreciate it.

  7. Hi there Dear, are you genuinely visiting this site regularly, if so afterward you will
    absolutely take pleasant experience.

  8. Hello! Do you know if they make any plugins to assist with SEO?

    I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains.
    If you know of any please share. Thanks!

  9. It’s remarkable to pay a visit this site and reading the views of all colleagues on the topic of this piece
    of writing, while I am also eager of getting knowledge.

  10. I’m impressed, I have to admit. Seldom do I come across a blog
    that’s both equally educative and amusing, and without a doubt, you have hit the
    nail on the head. The issue is something that too
    few men and women are speaking intelligently about.

    I am very happy that I stumbled across this during my hunt for something regarding this.

  11. Coconut Oil says:

    Hi there mates, its great post about cultureand fully explained, keep
    it up all the time.

  12. When I originally left a comment I appear to have clicked the -Notify me when new comments are added-
    checkbox and now whenever a comment is added I get 4 emails with the same comment.

    There has to be a way you can remove me from that service?
    Thank you!

  13. Sling TV says:

    Hi mates, good article and pleasant urging commented at this place, I am truly enjoying by
    these.

  14. Sling TV says:

    Hey there! I’ve been following your website for
    a while now and finally got the bravery to go ahead and give you a shout out from New Caney
    Texas! Just wanted to mention keep up the great job!

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image