提示:其实 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(i
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
从你的文章看得出你是个很好的人,呵呵
Hello, I enjoy reading all of your article post.
I like to write a little comment to support you.
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!
Thanks for sharing your thoughts about minecraft free download 2018.
Regards
Hey very nice blog!
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.
Hi there Dear, are you genuinely visiting this site regularly, if so afterward you will
absolutely take pleasant experience.
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!
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.
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.
Hi there mates, its great post about cultureand fully explained, keep
it up all the time.
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!
Hi mates, good article and pleasant urging commented at this place, I am truly enjoying by
these.
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!
I feel that is one of the most significant info for me.
And i’m satisfied studying your article. But want to observation on few normal issues, The web site style is
great, the articles is really excellent : D.
Good activity, cheers
Oh my goodness! Incredible article dude!
Many thanks, However I am encountering troubles with your RSS.
I don’t know why I cannot join it. Is there anybody having similar
RSS issues? Anybody who knows the solution can you kindly respond?
Thanks!!
Good information. Lucky me I came across your
blog by accident (stumbleupon). I have book marked it for later!
Way cool! Some very valid points! I appreciate you penning this post plus the rest of the website is
extremely good.
We stumbled over here from a different web address and thought I
might as well check things out. I like what I see so now i am following you.
Look forward to looking at your web page again.
What’s up, I log on to your blogs on a regular basis.
Your story-telling style is awesome, keep it up!
I am not sure where you are getting your information, but great topic.
I needs to spend some time learning more or understanding more.
Thanks for wonderful info I was looking for this
information for my mission.
Keep this going please, great job!
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
Your means of explaining the whole thing in this article is
genuinely nice, every one can easily understand it, Thanks a
lot.
Hello, I wish for to subscribe for this blog to take latest updates, thus where can i do it please help.
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.
Hi there! I know this is kinda off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form?
I’m using the same blog platform as yours and I’m having trouble
finding one? Thanks a lot!
Thanks for sharing your thoughts on gamefly. Regards
Hello! I know this is kinda off topic but I was wondering if you knew
where I could find a captcha plugin for my comment form?
I’m using the same blog platform as yours and I’m having trouble finding one?
Thanks a lot!
Wonderful work! This is the kind of information that
are supposed to be shared around the internet. Disgrace on the search engines for not positioning this put up upper!
Come on over and seek advice from my site . Thank you =)
Right here is the right blog for everyone who
would like to find out about this topic. You know so
much its almost tough to argue with you (not that I actually would
want to…HaHa). You certainly put a fresh spin on a
topic that’s been discussed for a long time. Excellent stuff, just excellent!
Superb, what a website it is! This webpage gives helpful facts to us, keep
it up.
Yesterday, while I was at work, my cousin stole my iphone and
tested to see if it can survive a 25 foot drop, just so she can be a youtube sensation. My
apple ipad is now broken and she has 83 views. I know
this is completely off topic but I had to share it with someone!
Hello there! I could have sworn I’ve visited this website before
but after going through some of the articles I realized it’s new to
me. Nonetheless, I’m certainly pleased I found it and I’ll be book-marking it and checking back
often!
Heya i am for the first time here. I came across this board and I find It really useful & it helped me out much.
I hope to give something back and aid others like you aided me.
I am in fact pleased to read this weblog posts which carries tons
of useful information, thanks for providing these data.
I do trust all the concepts you’ve introduced to your post.
They’re really convincing and can certainly work.
Nonetheless, the posts are very brief for beginners.
May just you please lengthen them a little from subsequent time?
Thank you for the post.
Hello! I could have sworn I’ve been to your blog before but
after browsing through some of the posts I realized it’s new to me.
Anyways, I’m certainly pleased I stumbled upon it and I’ll be bookmarking it and checking back often!
Wow, marvelous blog structure! How lengthy have you been blogging for?
you made blogging look easy. The whole look of your
web site is excellent, let alone the content!
What’s up Dear, are you in fact visiting this web page daily, if so
afterward you will definitely obtain good know-how.
Hi, Neat post. There is a problem along with your web site in web explorer, would check this?
IE nonetheless is the marketplace leader and a big component of people will pass over your great writing because of this problem.
If some one wishes expert view regarding blogging then i propose him/her to pay a quick
visit this web site, Keep up the pleasant job.
Hello! Do you use Twitter? I’d like to follow you if
that would be ok. I’m undoubtedly enjoying your blog and look forward to new updates.
you’re in point of fact a just right webmaster. The website loading speed is
amazing. It kind of feels that you’re doing any unique trick.
Moreover, The contents are masterpiece. you’ve done a great process in this subject!
Hi there, I enjoy reading all of your article.
I wanted to write a little comment to support you.
Thanks for the auspicious writeup. It in fact used to be a enjoyment account it.
Glance complicated to more delivered agreeable from
you! By the way, how could we keep up a correspondence?
Hello, after reading this remarkable paragraph i am as well
happy to share my experience here with friends.
We are a bunch of volunteers and starting a brand new scheme in our community.
Your site offered us with useful information to work on. You have done a formidable process and our entire group can be grateful to you.
An intriguing discussion is worth comment.
I do believe that you ought to publish more on this subject, it may not
be a taboo matter but typically folks don’t discuss such
subjects. To the next! Cheers!!
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 lot of time both reading and posting comments.
But so what, it was still worth it!
Can you tell us more about this? I’d care to
find out some additional information.
I am sure this post has touched all the internet viewers, its really really nice piece of writing on building up
new webpage.
I don’t even know how I stopped up here, however I assumed this post used
to be good. I do not recognize who you might be
but definitely you are going to a well-known blogger for those who aren’t already.
Cheers!
I am curious to find out what blog platform you have been working with?
I’m experiencing some minor security issues with my latest site and I’d like to find something more safeguarded.
Do you have any recommendations? natalielise pof
Hi, this weekend is nice in support of me, for the reason that this occasion i am reading this fantastic informative article here at my residence.
natalielise plenty of fish
Truly many of very good data.
cheap medications
online pharmacy
no prescription pharmacy
canadian pharmacies online
prescription price comparison – https://www.canadianpharmacycom.com/
northwest pharmacy/com Jen 42241cf
Howdy! This blog post couldn’t be written much better!
Going through this article reminds me of my previous roommate!
He constantly kept preaching about this. I most certainly will send this information to him.
Pretty sure he’s going to have a very good read.
Thank you for sharing!
Hey there! I’m at work browsing your blog from my new apple iphone!
Just wanted to say I love reading your blog and look forward to all your posts!
Keep up the fantastic work!
Its not my first time to pay a quick visit this web page,
i am visiting this website dailly and obtain pleasant facts
from here all the time.
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!
Thanks for sharing your thoughts about Free Watch Jav.
Regards
My family always say that I am killing my time here at web, but
I know I am getting familiarity all the time by reading thes good posts.
great issues altogether, you just received a new reader.
What might you recommend about your post
that you simply made a few days ago? Any certain?
whoah this blog is great i love studying your articles.
Stay up the good work! You realize, lots of persons are hunting around for this information, you could aid
them greatly.
I know this web page offers quality dependent articles or reviews and other
material, is there any other website which gives these kinds of information in quality?
Incredible! This blog looks just like my old one!
It’s on a totally different subject but it has pretty
much the same layout and design. Excellent choice of
colors!
This paragraph provides clear idea in support of the
new viewers of blogging, that actually how to do blogging and site-building.
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!
Hello there! Do you use Twitter? I’d like to follow you if that would be ok.
I’m definitely enjoying your blog and look forward to new posts.
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
Sweet blog! I found it while browsing on Yahoo News.
Do you have any suggestions on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get there!
Cheers
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!
Hi to every body, it’s my first pay a quick visit of this
website; this weblog consists of amazing and in fact good material for readers.
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!
We’re a group of volunteers and starting a new scheme in our community.
Your web site provided us with valuable info to work on. You’ve
done an impressive job and our whole community will be grateful to you.
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.
Great post! We are linking to this great post on our site.
Keep up the good writing.
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. . . . . .
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.
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.
I take pleasure in, result in I discovered just what I used to be looking for.
You have ended my 4 day long hunt! God Bless you man. Have a nice day.
Bye
My family every time say that I am wasting my time here at net, however I
know I am getting know-how daily by reading thes pleasant posts.
I’m gone to convey my little brother, that he should also
visit this blog on regular basis to obtain updated from most up-to-date gossip.
Very nice post. I certainly love this site. Keep writing!
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.