小试awk

       awk好久没用了,这些天处理文本用了一下,还是那么锐利。awk的高产,PATTERN-ACTION独特的文本行处理方式,规则表达式的匹配,C语言的语法,使之始终是我编程工具中必备的利器之一。贴一下:
注意:原始文件为:a.txt,b.txt,uk.txt,只引用其中一小部分, merge.txt为中间文件,illegal.txt为最终生成的文件。

a.txt
[ei]


[Ab]
[Ab5ekstrE]
[Abi5niFiEu]

[7Ab5EuvEu]
......................

b.txt
a
a few
a little
ab
ab extra
ab initio
ab intra
ab ovo
..............................

#merge.awk
#ARGV[1] is dictionary tones, ARGV[2] is words list.
#merge the two files except blank tones in ARGV[1]
BEGIN {
    ORS="";
    while(getline < ARGV[1] > 0)
    {
        tone = $0 #store the line
        getline < ARGV[2]
        if(length(tone) > 0)
        {
            print tone "/t/t/t" $0
            print "/n"
        }
    }
}


$ awk  -f  merge.awk a.txt b.txt>merge.txt

生成的中间文件下面要用到:
merge.txt
[ei]            a
[Ab]            ab
[Ab5ekstrE]            ab extra
[Abi5niFiEu]            ab initio
[7Ab5EuvEu]            ab ovo
.......................................

uk.txt
ii    i:
aa    B:
oo    C:
uu    u:
@@    /:
......................................

#find.awk
#check illegal dictionary tones(illege is defined as this: the tones can't find in tts lists(us.txt))
BEGIN {
    while(getline<ARGV[1] > 0)
        tone[$2]++
   
    while(getline<ARGV[2] >0)
    {
         for(i = 1; i <= length($1); i++)
        {
            c1 = substr($1, i, 1)  #tone charaters
            c2 = substr($1, i, 2)  #two charaters
           
            if(c2 in tone)
                i++
            else if(c1 in tone)
            {
                c2 = substr($1, i, 2)
                if(c2 in tone)
                    i++
            }
            else if(c1 != "[" && c1 != "]" && c1 !~ /[57]/)
            {
                if(c1 == ":")
                    c1 = substr($1, i-1, 2);
                print c1 "/t" $0
            }
        } 
    }
}


$awk -f  find.awk uk.txt merge.txt>illegal.txt

最终生成的文件
illegal.txt
[:    [:blQdsQkE]            bloodsucker
B    [5blBuzi]            blowsy
B    [5dVBki]            jockey
B    [5glEub7flBuE]            globeflower
B    [5hBundztQN]            hound's-tongue

2、背单词老是按1个次序背效果不好,下面的小程序可以用来打乱次序:

#This program is used to rearrange a text in random way.

{   
    lines[NR] = $0           #read the text into array line by line  (i->line), NR increments by one from one.
}

END{
    seed = srand()        #set the seed with current time if pass empty parameter to srand, return older seed.
    #print seed
    for(i = 0; i < FNR; i++)
    {
        while(1)
        {
            r = int(rand()*FNR) + 1 #get a random r in range(1-FNR)
            if(r in lines)        #if r exist in array, print it and delete it.
            {
                #print r" --------- "lines[r]
                print  lines[r]
                delete lines[r]
                break
            }
            #print r
        }
    }   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值