awk用法总结

时间:2024.5.13

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<20xx) c1+=$4;

else if ($4>20xx&&$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<NF) {print NF,$i;i++}}' file 通过while语句实现循环。 awk '{ for(i=1;i<NF;i++) {print NF,$i}}' file 通过for语句实现循环。 type file|awk -F "/" '

{ for(i=1;i<NF;i++)

{ if(i==NF-1) { printf "%s",$i }

else { printf "%s/",$i } }}' 显示一个文件的全路径。

用for和if显示日期

awk 'BEGIN {

for(j=1;j<=12;j++)

{ flag=0;

printf "\n%d月份\n",j;

for(i=1;i<=31;i++)

{

if (j==2&&i>28) 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


第二篇:as用法总结


高考常见词as用法总结

近几年高考中有关as一词的题目出现过很多次,其中as或是最佳选项,或是干扰项,或是题干中的关键信息点,涉及as作为连词、介词、关系代词、副词以及习语等各种用法。现结合高考试题将as的用法归纳如下:

  一、 用作连词的as

  1. 引导时间状语从句,表示“当……的时候”,其用法与when和while类似。例如:As a child (When he was a child), she was sent to six different schools. 此用法中as多表示从句与主句动作同时进行,一般不与状态动词连用,意为“一边……一边……”。例如:She sang songs as she did her homework.

  as 还可以表示一动作紧接着另一动作发生,说明另一动作的结果,有“随着……”的含义。例如:As time went by, we found he was an honest man./As he grew older, he lost interest in everything except gardening.

  例1 It wasn’t until nearly a month later________ I received the manager’s reply.

  A. since   B. when

  C. as       D. that

  解析:本题中的as与when一起作为干扰项,容易使同学往时间状语从句上思考,事实上本题是强调句,正确选项是D。

  2. 引导原因状语从句,表示“由于,因为”,相当于because, 但通常置于句首。例如:As he is a qualified doctor, I trust his advice on medical matters.

  例2 _____ modeling business is by no means easy to get into, the good model will always be in demand.

  A. While  B. Since

  C. As      D. If

  解析:本题中as干扰性很强,很多同学误以为是原因状语从句,但仔细分析不难发现正确答案应是A(while表示“只要”)。

  3. 引导让步状语从句,通常可与although或though通用,但语序不同,although或though用于句首,as用于倒装结构。例如:Young as I am, I already know what career I want to follow. /Although(或Though)I am young, I already know what career I want to follow.

  as 表示让步用于倒装结构,通常将从句的表语、状语或动词前置。如果表语有冠词a/an, 需去掉。例如:Great scholar as he is, he is lacking in common sense./Much as I like you, I couldn’t live with you. / Try as he would, he couldn’t

open the door.

  例3 _____ he has limited technical knowledge, the older worker has a lot of experience.

  A. Since  B. Unless

  C. As      D. Although

  解析: as 虽然有引导让步状语从句的用法,但本题不是倒装结构,故正确答案应是D。

  4. 引导方式状语从句,表示“如,像”。例如:When in Rome, do as Romans do./Do to others as you would have others do to you.

  例4 We wanted to get home before dark, but it didn’t quite ______ as planned.

  A. make out    B. turn out

  C. go on  D. come up

  解析: 本题考查动词词组辨析,题干中的“as planned”提供了重要信息,答案选B。

  5. 固定句型:“主句,as +be/do+主语”表示“也一样”。例如:She’s unusually tall, as are both her parents./He’s a doctor, as was his wife before she had children.

  二、 用作介词的as

  1. 表示“如,像”。例如:They got united as one man./She spoke of me as her dearest friend.

  2. 表示“作为,当作”。例如:As a League member, you should think more of others./After graduating from the college, he firstly worked as a clerk in a big company.

  3. 与某些动词搭配,表示“把……当作……”,如:look on...as..., regard...as..., treat...as..., consider...as..., think of...as..., see...as...等。其中consider...as...中的as可以省略。as与famous或known搭配,表示“作为……而出名”。

  例5 Linda worked for the Minnesota Manufacturing and Mining Company, ________ as 3M.

  A. knowing     B. known

  C. being known      D. to be known

  解析: 如果熟悉be known as这一短语,运用有关非谓语动词的常识,就可选出正确答案B。

  三、 用作关系代词的as

  1. 引导限制性定语从句,先行词前通常有as, so, such, same等修饰语。例如:He will marry as pretty a girl as he can find./My hometown is no longer the same as it used to be./As many people as are present will be given a present.在此种用法中,同学们要注意与结果状语从句的区别。比如:① The teacher asked us such a difficult question that none of us could answer it.② The teacher asked us such a difficult question as none of us could answer.①句为结果状语从句,而②句则是定语从句。

  2. 引导非限制性定语从句,用来指代整个主句(即先行句),表示“这一事实,那一情况”。从句可以位于句首、句中或句末。例如:We stand when the national anthem is played, as is the custom.

  例6 ______ I explained on the telephone, your request will be considered at the next meeting.

  A. When  B. After

  C. As      D. Since

  解析: 根据句意,选项C是正确答案。这里as体现的正是本点所讲用法。

  例7 _______ is our belief that improvements in health care will lead to a stronger, more prosperous economy.

  A. As      B. That

  C. This    D. It

  解析: 本题中的as项干扰性很强,容易误选。实际上,句中that引导的应是真正的主语从句,it充当形式主语,故选D。如果将题干中的that换为逗号,则必须选as。

  例8 Danby left word with my secretary ________ he would call again in the afternoon.

  A. who    B. that

  C. as       D. which

  解析: that引导的从句是word的同位语,本题应选B。as无此用法,在这里起干扰作用。

  四、 用作副词的as

  修饰形容词或副词,表示程度,意为“同样地”。例如:He swims fast, but I swim just as fast. 但它通常构成表示比较的结构as...as...,not as...as...。此结构中第一个as是副词,第二个as是连词。否定结构中的副词as可以由so代替。as...as possible和as...as one can也属于这种用法。例如:It is generally believed that teaching is as much an art as it is a science./The movie was not as good as I had expected./I play tennis as well as him.

  五、 用在习语中的as

  由as构成的习语很多,常见的有:as soon as “一……就”,引导时间状语从句;as(或so) long as “只要”,引导条件状语从句;as if/though “好像,仿佛”,引导方式状语从句或表语从句;as to(或as for) “至于,就……而言”;as much(或many) as“多达……”;as(或so) far as “就……的限度”;as a result of “(由于……的)结果”;as a matter of fact“事实上”;as well “也,还”;A as well as B“不但A而且B”;as it is“照现状看,看样子”,等等。这些习语在高考中出现频率较高,有的作为正确选项,有的作为干扰项,有的出现在题干。例如:

  例9 I would like a job which pays more, but ______ I enjoy the work I’m doing at the moment.

  A. in other words

  B. on the other hand

  C. for one thing

  D. as a matter of fact

  解析: 选项D极具干扰意义,进一步研读会发现本题中的两个分句表达了一件事的两个对立面,故选B。

  例10 —People should stop using their cars and start using public transport.

    —______. The roads are too crowded as it is.

  A. All right      B. Exactly

  C. Go ahead    D. Fine

  解析: 本题的as没有出现在选项中,但题干中的as it is是解题的关键信息,正确选项为B。

  例11 Maggie has been fortunate to find a job she loves and, _____, she gets well paid for it.

  A. sooner or later

  B. what’s more

  C. as a result

  D. more or less

  解析: 本题的后一个分句不表示结果,而是递进,故选B而不是C。

  例12 The winter of 1990 was extremely bad. _______ most people say it was the worst winter of their lives.

  A. At last B. In fact

  C. In a word   D. As a result

  解析: 选项D起干扰作用,正确选项为B。

以上总结的是as一词在教材中出现的主要用法及在高考卷中的具体应用,可以看出它词性多,词义广,用法灵活。同学们在复习中一定要理清思路,抓住重点,应用时仔细分析上下文,弄清逻辑关系,才能作出正确选择。

“ as...as... ”的结构的理解和翻译

  “ as …… as …… ”是大家熟悉的一种结构,常译为“像……一样”。但在实际应用中,会有例外。如在《大学体验英语》中有这样一个句子:

  Anti-virus software plays one, albeit important, part in the defense of your company from malicious attack but the security of your computer system is only as strong as the weakest link. And that, more often than not, is the human factor.

  在这里,将 as …… as …… 译为“像……一样”,显然是行不通的。如何解决这一问题?我们可以从 as …… as …… 的结构入手,分析一下它的用法。

  1. 当主语是同一性质的事物比较时, as …… as …… 解释为“同……一样”

  例: John is as bright as Bob. 约翰和鲍博一样聪明。

  2. 当 as …… as …… 结构的主语相同,而比较项目不同,即把同一事物两种不同性质的方面等同比较时,相当于 both …… and …… ,可译为“既……又……”

  例: He is as brave as wise. 他既勇敢又聪明。

  She is as gifted as diligent. 她不但有天赋,而且勤奋。

  根据以上讨论,我们可以将上面一个句子译为:“杀毒软件的作用固然重要,但在保护公司不受恶意攻击的战斗中,只能起一定的作用。公司的计算机系统安全与否,恰恰系于一个既薄弱又牢固的环节,而这个环节往往取决于人。”

  除此以外, as …… as…… 还有以下几种用法:

  1. as …… as …… 结构的两个主语和两个比较项目都不同,即把两个不同的人和物在不同的方面比较时,相当于 whereas ,可译为“而”。

  例: He was as experienced as his brother was green. 他经验丰富,而他的弟弟则涉世未深。

  The prisons are as over-crowded as the farmlands are empty. 监狱里人满为患,而地里却无人耕作。

  2. 当 as …… as …… 的结构后面连接 can / any / ever / possible / 等词时,表示最高级的意思,可译为“极其,最好或尽量”。

  例: He is as wrong as wrong can be. 他大错特错了。

  This is as fair a land as ever. 这是一片最美的土地。

  3. 当 as …… as …… 结构后面表示重量、数量、时间、距离等计量名词时,相当于 up to ,表示程度,可译为“达到……”。

  例: The river is as deep as 10 meters. 此河深达 10 米。

  At night, the temperature on the moon may be as low as -160 ℃ . 在夜晚,月球的温度可能低至零下 160 度。

as …… as …… 结构还有一些固定搭配的词组,其中意思也难于从字面上猜测,如 as good as 简直是 / 几乎等于, as soon as not 更愿意, as likely as not 很可能, as often as not 往往 / 常常,这些需要平时多加积累和注意。

关于as...as 结构的几个句式

 在讲到形容词比较级as...as 时,绝大多数讲的都是 as +形容词+ as 结构。例如:

 He is as handsome as his brother (is).  他和他哥哥一样英俊。

Judy goes to the movies as often as Sara does.  朱迪像莎拉一样时常去看电影。

Old John goes to sleep as easily as a baby does. 老约翰像婴儿一样容易入睡。

He cannot run so/as fast as you. 他不能有你跑得那么快。

但是,像上面那样的例子多了,就产生一个问题:不少学生都以为as ...as 中间只有形容词。实际上,在 as...as 结构中,还可以有其它的几个句式。

 1、 as 形容词+名词 as 句式

 这种结构在文章中也很常见,但很多学生不习惯。这里给几个例子,请大家仔细研究一下,提一点请大家注意,就是:形容词后面(包括many/much) 的名词有单数、有复数、有不可数。例如:

 This is as good an example as the other is. 这和另外一个一样是个好例子。

He is as nice a boy as Peter (is).  他和彼得一样都是好男孩。

The girls are as busy with their work as bees. 这些女孩子就跟蜜蜂一样忙着工作。

 She has as many friends as Mary (does).  她和玛丽一样有许多朋友。

He has as much money as John (does).  他和约翰一样有很多钱。

I can carry as much paper as you can. 你能搬多少纸,我也能搬多少。

 2、as many as 与 as much as

 这两个比较的意思是“像……一样多”,既可以把它们作为比较级看,也可以把它们看作是一个固定的词组。同样,在 ...many/much... 后面可以插入名词,形成 as...many/much +名词...as 的句式。例如:

 (1) 作为固定词组来使用的,相当于“……那么多”。

 She has as many as seven sisters.  她的姊妹有七人之多。

As many as 100 people were killed in the air crash. 这次空难中多达一百人丧生。

I gave the beggar as much as 1,00 yen.   我给了这乞丐整整一百元。

Building costs ran up to as much as $ 20 million.  建筑费用涨到高达两千万。

 类似的“长达……”、“宽达……”、“早在…”等,也可以用这种结构表示。例如:

 I have been learning English (for) as long as 15 years. 我学英文已有十五年之久。

He came home as late as two in the morning.  他迟至清晨两点才回家。

I saw your brother as late as last week. 上个礼拜我还看到你哥哥。

The river is as wide as 100 meters.  这条河宽达一百公尺。

I mailed the letter as early as Friday.  我早在星期五就把信寄了。

We walked as far as the railroad station. 我们走到火车站那样远的地方。

 (2) 作为比较级来使用的,后面可以插入名词,意思是“……一样”。

 To eat too much is as much a bad thing as to eat nothing. 吃太多和什么都不吃同样是坏事。

He is as much a member of the orchestra as Tom.  他和汤姆同样是管弦乐队的成员。

Comets are just as much members of the sun’s family as (are) the other planets. 彗星和其他的行星同样是太阳家族的成员。

 3、as...as one can(could) / as...as possilbe /  as ... as ... can be

 这几个结构意思差不多,都表示“尽可能”的意思。 例如:

 (1) as..can

 Take as much exercise as you can.  尽可能多做运动。

Be as careful as you can.  尽可能小心。

You should be as careful as you can in making friends. 在交朋友方面,你应该尽可能小心。

To speak English fluently, you had better read as many English articles as you can.

要想把英文讲得很流利,你最好尽量多看英文文章。

We had better learn by heart as many sentence patterns as we can. 我们最好尽可能多背句型。

I will try to save as much as I can.  我会设法尽可能多储蓄。

 (2) as..could

 Our teacher explained the lesson to us as clearly as he could.

我们的老师尽可能清楚地为我们解释这一课。

She needed to soften her request to make it as polite and courteous as she could.

她需要使请求语气柔婉,以便尽可能把它变成有礼貌与客气。

I looked down the road as far as I could.  我尽可能遥望这条路。

 (3) as...as can be

 His failure is as plain as plain can be.  他的失败是再明白不过了。

He is as handsome as (handsome) can be.  他潇洒极了。

She is as kind as can be.  她极为和颜悦色。

Mary is as hardworking as can be.  玛丽在班上之用功不输任何人。

He may not be so clever as Peter, but he is as industrious as can be. 他也许不如彼得聪明,但却用功极了。

 其它例如:

as plain as plain can be(再明白不过了)

as wise as wise can be(再聪明不过了)

as white as white can be(白得不能再白了)

as wrong as wrong can be(错得太离谱)

4、as ... as ever

as ever 可以当成一个词组,意思是“与以往一样”。例如:

He seems to be as busy as ever.  他似乎和往常一样忙碌。

He works as hard as ever. 他和往常一样努力工作。

She was as beautiful as ever after so many years.  过了那么多年,她美丽如故。

更多相关推荐:
awk用法小结

awk用法小结awk用法awk39patternaction39变量名含义ARGC命令行变元个数ARGV命令行变元数组FILENAME当前输入文件名FNR当前文件中的记录号FS输入域分隔符默认为一个空格RS输入...

awk文本处理总结

awk文本处理总结入门中级高级awk处理文本总结AlexWang20xx2191555作为技术支持工程我们最最经常的工作就是经常碰到要处理文本文件不管是什么数据库最后都可以导成文本我们就可以对他进行处理了这样即...

sed与awk用法对比总结

命令格式sed选项定址操作文件awk选项patternaction文件定址操作合称命令command简单的多操作格式sed选项定址操作操作第2个操作对所有输入行都执行awk选项patternactionacti...

awk 内置变量使用介绍

awk内置变量使用介绍awk是个优秀文本处理工具可以说是一门程序设计语言下面是awk内置变量一内置变量表属性01nFSNFNRRSOFSORSARGCARGVFILENAME说明当前记录作为单个变量当前记录的第...

sed awk工具语法基本使用方法详解(很好)

sed工具简介在了解了一些正规表示法的基础应用之后再来呢呵呵两个东西可以玩一玩的那就是sed跟awk了这两个家伙可是相当的有用的啊举例来说鸟哥写的logfilesh分析登录文件的小程序绝大部分分析关键词的取用统...

Linux第8次课:awk使用2

1什么是AWKawk是一个文本流编辑工具能够对文本流实现编程控制2awk命令格式及调用方式awkoption脚本内容文件名第一种调用方式awkoptionawk脚本语句文件名称第二种调用方式awkoptionf...

awk使用手册

什么是awk你可能对UNIX比较熟悉但你可能对awk很陌生这一点也不奇怪的确与其优秀的功能相比awk还远没达到它应有的知名度awk是什么与其它大多数UNIX命令不同的是从名字上看我们不可能知道awk的功能它既不...

awk使用大全

AIX上使用cpio备份数据cpio命令把文件从cpio归档中拷贝进出cpio归档可能跨越多个卷io和p标志选择了要执行的操作注如果您把输出从cpio命令重定向到一个特殊文件设备您应该把输出重定向到一个原始设备...

xcap使用小结1.3

Xcap使用小结gyb山穷水复Xcap是一款比较好用的抓包和发包工具只需将文件解压后就可以使用先安装Wincap只要装了wireshark都会有这个软件一第一次打开xcap时会显示加载宝文件失败是因为默认设置了...

Android WebView使用总结

AndroidWebView使用总结1WebView中shouldOverrideUrlLoading和onPageStarted方法的区别WebView中的shouldOverrideUrlLoading和o...

工作流Activiti的学习总结(二)activiti中ProcessEngine的创建和数据库表命名规则

工作流Activiti的学习总结(二)activiti中ProcessEngine的创建和数据库表命名规则数据库表命名规则:Activiti工作流引擎的数据库表中的表名称都是以ACT_.第二部分两个字母表示表的…

工作流Activiti的学习总结(四)Spring和Activiti的整合配置讲解

工作流Activiti的学习总结(四)Spring和Activiti的整合配置讲解Spring和Activiti的整合:在Spring和Activiti的整合中ProcessEngineFactoryBean成…

awk使用总结(12篇)