亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
樓主: ghostgorst
打印 上一主題 下一主題

awk的next使用 [復(fù)制鏈接]

論壇徽章:
0
21 [報(bào)告]
發(fā)表于 2009-08-21 14:09 |只看該作者

回復(fù) #19 kwokcn 的帖子

awk '/^a/{print $0} /^a/{print $0}' 那這樣不加next語句,awk怎么處理呢?
1.讀入一行,從第一個(gè)模式開始匹配一直到最后一個(gè)模式,所有模式都匹配一邊么?
2.還是處理一個(gè)模式時(shí)候,把所有匹配行都讀入處理,然后再處理下一個(gè)模式?

按照大家回帖我理解多數(shù)都是第1種情況,但想不出來例子證明這點(diǎn).

論壇徽章:
0
22 [報(bào)告]
發(fā)表于 2009-08-21 14:19 |只看該作者
偽碼來表示完整的AWK處理流程:


for (i = 0; i <啟動(dòng)參數(shù)中 -v Assignment 數(shù)目; i++)
{
    執(zhí)行第 i 個(gè) -v Assignment,為相應(yīng)的變量賦值
}

for (i = 0; i < BEGIN模式的數(shù)目; i++)
{
    處理第i個(gè)BEGIN模式對應(yīng)的操作
}

for (i = 0; i < 啟動(dòng)參數(shù)中 File Assignment 參數(shù)的總個(gè)數(shù); i++)
{

if (
i 個(gè)參數(shù)是 Assignment)


{

        執(zhí)行對應(yīng)的Assignment,為相應(yīng)的變量賦值

continue;


}


    打開第i個(gè)參數(shù)對應(yīng)的待處理文件

while ( not
文件結(jié)束)


{



根據(jù)RS,從待處理文件中讀取一條記錄



根據(jù)FS,將讀取的記錄分隔成不同的字段


for (j = 0; j < pattern
的數(shù)目; j++)


{


if (
j個(gè)pattern匹配成功) //如果沒有指定pattern,則總是匹配成功


{

                執(zhí)行對應(yīng)的action
//
如果沒有指定action,缺省為 print $0


}


}
//
所有pattern匹配結(jié)束


}
//
一個(gè)文件處理結(jié)束

}
//
所有文件都處理結(jié)束


for (i = 0; i < END模式的數(shù)目; i++)
{
    處理第i個(gè)END模式對應(yīng)的操作
}

論壇徽章:
23
15-16賽季CBA聯(lián)賽之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午馬
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16賽季CBA聯(lián)賽之山東
日期:2017-12-21 16:39:1915-16賽季CBA聯(lián)賽之廣東
日期:2016-01-19 13:33:372015亞冠之山東魯能
日期:2015-10-13 09:39:062015亞冠之西悉尼流浪者
日期:2015-09-21 08:27:57
23 [報(bào)告]
發(fā)表于 2009-08-21 14:20 |只看該作者

回復(fù) #20 ghostgorst 的帖子

man awk

AWK PROGRAM EXECUTION
       An  AWK program consists of a sequence of pattern-action statements and
       optional function definitions.
              pattern   { action statements }
              function name(parameter list) { statements }
       Gawk first reads the program source from the program-file(s) if  speci-
       fied, from arguments to --source, or from the first non-option argument
       on the command line.  The -f and --source options may be used  multiple
       times  on  the command line.  Gawk reads the program text as if all the
       program-files and command  line  source  texts  had  been  concatenated
       together.   This  is  useful  for  building libraries of AWK functions,
       without having to include them in each new AWK program that uses  them.
       It also provides the ability to mix library functions with command line
       programs.
       The environment variable AWKPATH specifies a search path  to  use  when
       finding  source  files named with the -f option.  If this variable does
       not exist, the default path is ".:/usr/local/share/awk".   (The  actual
       directory  may  vary, depending upon how gawk was built and installed.)
       If a file name given to the -f option contains a "/" character, no path
       search is performed.
       Gawk executes AWK programs in the following order.  First, all variable
       assignments specified via the -v option are performed.  Next, gawk com-
       piles  the program into an internal form.  Then, gawk executes the code
       in the BEGIN block(s) (if any), and then proceeds  to  read  each  file
       named  in  the  ARGV array.  If there are no files named on the command
       line, gawk reads the standard input.
       If a filename on the command line has the form var=val it is treated as
       a  variable  assignment.   The  variable var will be assigned the value
       val.  (This happens after any BEGIN block(s) have been  run.)   Command
       line  variable assignment is most useful for dynamically assigning val-
       ues to the variables AWK uses to  control  how  input  is  broken  into
       fields  and records.  It is also useful for controlling state if multi-
       ple passes are needed over a single data file.
       If the value of a particular element of ARGV is empty (""), gawk  skips
       over it.
       For  each record in the input, gawk tests to see if it matches any pat-
       tern in the AWK program.  For each pattern that the record matches, the
       associated  action  is  executed.  The patterns are tested in the order
       they occur in the program.

       Finally, after all the input is exhausted, gawk executes  the  code  in
       the END block(s) (if any).

論壇徽章:
0
24 [報(bào)告]
發(fā)表于 2009-08-21 14:34 |只看該作者

回復(fù) #20 ghostgorst 的帖子

是第一種,每讀一行做一遍處理。

這個(gè)貌似沒必要證明吧……如果非要確定,一個(gè)簡單的方式就是在awk內(nèi)部沒有存儲(chǔ)操作的前提下,直接看一下awk占用的內(nèi)存是否線性增長就行了。

論壇徽章:
0
25 [報(bào)告]
發(fā)表于 2009-08-21 14:41 |只看該作者

回復(fù) #23 kwokcn 的帖子

感謝各位幫忙了.非常感謝

論壇徽章:
0
26 [報(bào)告]
發(fā)表于 2009-08-21 14:43 |只看該作者

回復(fù) #21 wwmstone 的帖子

謝謝大你的偽代碼了.

論壇徽章:
0
27 [報(bào)告]
發(fā)表于 2009-08-21 14:49 |只看該作者
先說你的后面兩個(gè)問題,為了方便,print $0都寫成print(因?yàn)樾Ч粯?
2,awk '/^a/{print ;next}' t
如果匹配以a開頭則順序進(jìn)行花括號(hào)里面的操作,而匹配以a開頭的記錄后的默認(rèn)操作是print 然后讀入下一條記錄(next),  
因此,這個(gè)花括號(hào)和里面的東西都是多余的,效果和awk '/^a/' t 一樣。 讀兩個(gè)文件也一樣的道理

3,awk '/^a/{next;print}' t
道理跟上面說的一樣,如果匹配以a開頭的行,則進(jìn)行{}里的操作(也就是說{}里的操作都是在匹配/^a/的前提下進(jìn)行的)
里面的print是沒有意義的。因?yàn)樵趐rint之前已經(jīng)next(讀下一記錄)了,下一條記錄不匹配以a開頭的話就不進(jìn)行任何操作(默認(rèn))
匹配的話就next了。

你的本意應(yīng)該是想用awk '/^a/{next;print}' 來輸出不匹配以a開頭的行,這是不正確的,因?yàn)槟銢]有為不匹配以a開頭的行
指定一個(gè)動(dòng)作。因此得到的是什么都沒有。應(yīng)該改成這樣來輸出不以a開頭的行
awk '/^a/{next}{print}' t
或者
awk '/^a/{next}1' 1表示默認(rèn)打印操作

第一個(gè)問題
1 ,awk '/^a/{print;next}/^a/{print}' t
在這里兩個(gè)pattern都是一樣的,awk只認(rèn)為他們是一個(gè),但是操作是連續(xù)起來的
相當(dāng)于
awk '/^a/{print;next;print}' t   
這個(gè)時(shí)候很清楚了,后面一個(gè)print沒有意義了。
awk '/^a/{print}/^a/{print}' t  <=>  awk '/^a/{print;print}' t
awk '/^a/{print}/^b/{print}/^a/{print}' t  <=> awk '/^a/{print;print}/^b/{print}' t

論壇徽章:
0
28 [報(bào)告]
發(fā)表于 2009-08-21 15:01 |只看該作者

回復(fù) #26 lucash 的帖子

awk '/^a/{print}/^b/{print}/^a/{print}' t  <=> awk '/^a/{print;print}/^b/{print}' t


這個(gè)不大對吧?

telfort1:/kfbuild/x144378>cat file
a
b
a
c
d
e
f
如果向你說的那樣,模式相同的只保留一份模式.

telfort1:/kfbuild/x144378>awk '/^a//^b//^a/' file
a
a
b
a
a

結(jié)果應(yīng)該是
a
a
a
a
b
才對吧?

論壇徽章:
0
29 [報(bào)告]
發(fā)表于 2009-08-21 15:11 |只看該作者
原帖由 ghostgorst 于 09-8-21 15:01 發(fā)表
awk '/^a/{print}/^b/{print}/^a/{print}' t   awk '/^a/{print;print}/^b/{print}' t


這個(gè)不大對吧?

telfort1:/kfbuild/x144378>cat file
a
b
a
c
d
e
f
如果向你說的那樣,模式相同的只保留一 ...


我這里是這樣的:(你那里 awk '/^a//^b//^a/' t 不報(bào)錯(cuò)嗎)

Crys t:# cat k
a
b
a
c
d
e
f
Crys t:# awk '/^a/{print}/^b/{print}/^a/{print}' k
a
a
b
a
a
Crys t:# awk '/^a/{print;print}/^b/{print}' k
a
a
b
a
a

論壇徽章:
0
30 [報(bào)告]
發(fā)表于 2009-08-21 15:15 |只看該作者

回復(fù) #28 lucash 的帖子

awk '/^a//^b//^a/' file
我這沒出錯(cuò)啊,不是說模式的默認(rèn)動(dòng)作是打印么?我就沒寫.

反正沒出現(xiàn)
a
a
a
a
b
這樣的結(jié)果!
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP