收集整理的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

1 thought on “收集整理的awk用法小结”

  1. 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!

  2. 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.

  3. I think this is one of the most important info for me. And i’m happy
    reading your article. However want to commentary on few general issues, The web site taste is perfect, the articles is truly great :
    D. Just right process, cheers

  4. May I simply just say what a comfort to uncover someone who actually knows what they are talking about online.

    You definitely realize how to bring a problem to
    light and make it important. A lot more people ought to look at this and
    understand this side of the story. I can’t believe you are not more popular since you
    most certainly possess the gift.

  5. Have you ever thought about adding a little
    bit more than just your articles? I mean, what you say is fundamental and all.
    However think about if you added some great pictures or videos to give your posts more, “pop”!
    Your content is excellent but with images and clips, this site could undeniably be
    one of the most beneficial in its field. Good blog!

  6. Hey would you mind stating which blog platform you’re working with?
    I’m going to start my own blog in the near future but I’m having a difficult time deciding between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your layout seems different then most blogs and
    I’m looking for something completely unique.
    P.S My apologies for being off-topic but I had to ask!

  7. I don’t know whether it’s just me or if perhaps everybody else encountering
    problems with your website. It appears like some
    of the written text within your posts are running
    off the screen. Can somebody else please comment and let me know if
    this is happening to them as well? This could be a problem with my internet browser because I’ve had
    this happen before. Many thanks

  8. What i don’t understood is actually how you are now not actually much more
    neatly-appreciated than you may be now. You’re
    so intelligent. You recognize thus significantly in terms of this subject, produced me personally consider it from so many varied angles.
    Its like men and women are not involved until it is one thing
    to accomplish with Girl gaga! Your personal stuffs great.
    Always care for it up!

  9. Having read this I believed it was extremely enlightening.
    I appreciate you spending some time and energy to put this content together.
    I once again find myself spending a significant amount
    of time both reading and posting comments.
    But so what, it was still worth it!

  10. Greate post. Keep writing such kind of info on your page.
    Im really impressed by your site.
    Hi there, You’ve performed an excellent job. I will certainly
    digg it and personally recommend to my friends.
    I’m confident they will be benefited from
    this site.

  11. Hey very nice blog!! Man .. Excellent .. Wonderful ..
    I’ll bookmark your blog and take the feeds also? I’m happy to seek out a lot
    of helpful information right here in the publish, we’d like
    work out extra techniques in this regard, thank
    you for sharing. . . . . .

  12. Can I just say what a relief to uncover somebody who really understands what they are talking about online.

    You actually understand how to bring an issue to light and make it important.
    More and more people must look at this and understand this side of
    your story. I was surprised you’re not more popular
    since you surely have the gift.

  13. Simply want to say your article is as astonishing.
    The clarity on your submit is simply cool and
    i could assume you’re knowledgeable on this subject.
    Well with your permission allow me to seize your feed to stay updated with drawing close post.
    Thank you one million and please continue the gratifying work.

  14. Do you have a spam problem on this website; I also am a blogger, and I was wanting to know your situation;
    many of us have created some nice procedures and we are looking
    to exchange methods with others, be sure to shoot me an e-mail if interested.

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