尝试将一个简单的 C 程序转换为 AWK 程序

尝试将一个简单的 C 程序转换为 AWK 程序

大家好,几周前我写了一个 C 程序,提示用户输入一个文本文件的名称,然后提示用户输入一个单词。然后,该程序输出输入的文本文件,并在文本左侧显示数字,并输出该单词在文本文件中出现的次数。它还输出单词所在的匹配行号。

下面是实际操作的一个示例:

Enter the name of a text file: **bond.txt**
Enter the pattern to search for: Bond
File contents:
1) Secret agent Bond had been warned not to tangle with Goldfinger.
2) But the super-criminal's latest obsession was too strong, too dangerous.
3) He had to be stopped.
4) Goldfinger was determined to take possession of half the supply of
5) mined gold in the world--to rob Fort Knox!
6) For this incredible venture he had enlisted the aid of the top
7) criminals in the U.S.A, including a bevy of beautiful thieves from the
8) Bronx. And it would take all of Bond's unique talents to make it fail--
9) as fail it must.
There is a match on line 1
There is a match on line 8
'Bond' appeared 2 times in the file bond.txt.

目前,我正在尝试通过重复我在 C 中编写的程序但使用 awk 来练习 awk 编程。

以下是我目前能想到的:

BEGIN{
    printf("Enter filename : ")
    getline file < "-"
    while((getline < file)) {
        {print "File Contents:"}
        {printf("%5d) %s\n", NR,$0)}
    }
}

这绝不是完整的,但最好的方法是什么,可以让我逐行解析文本文件以搜索用户输入的单词。有什么提示或技巧吗?谢谢。

答案1

如果只是搜索匹配项以及哪些行包含匹配项之类的简单操作,那么awk最好将文件名作为命令行参数提供。在纯 awk 中,这将是这样的

awk '/searchTerm/ {print "Match on " NR }' inputFile.txt

但可以肯定的是,您的程序可以稍加编辑,然后转换为 AWK 脚本来完成相同的工作,但代码行数更多。唯一的小问题是,一旦文件读取完毕,必须按 Ctrl+D 来停止 while 循环。

#!/usr/bin/awk -f 

BEGIN{
    printf("What do we search for:")
    getline searchTerm
    printf("Enter filename : ")
    getline file < "-"
    print "File Contents:"

    while((getline < file)) {       
        x++
        printf("%d) %s\n",x,$0)
        if ($0 ~ searchTerm)  {i++;array[i] = x}
    } 
    #  end while statement
}

END{
    print "There were "i" matches on lines:"
    for (j=1;j<=i;j++) printf array[j]" ";
}

下面您将看到演示。将上面的“翻译”代码与纯 AWK 代码进行比较:

xieerqi:$ ./testAwk.awk

What do we search for:wlan0
Enter filename : inputFile.txt
File Contents:
1) [33330.084088] wlan0: associate with 00:24:37:10:9c:10 (try 1/3)
2) [33330.086392] wlan0: RX AssocResp from 00:24:37:10:9c:10 (capab=0x411 status=0 aid=1)
3) [33330.086432] wlan0: associated
4) [40272.298739] audit: type=1400 audit(1447859037.270:65): apparmor="STATUS" operation="profile_replace" name="/usr/lib/cups/backend/cups-pdf" pid=4082 comm="apparmor_parser"
5) [40272.298753] audit: type=1400 audit(1447859037.270:66): apparmor="STATUS" operation="profile_replace" name="/usr/sbin/cupsd" pid=4082 comm="apparmor_parser"
6) [40272.299725] audit: type=1400 audit(1447859037.270:67): apparmor="STATUS" operation="profile_replace" name="/usr/sbin/cupsd" pid=4082 comm="apparmor_parser"
7) [59169.780814] atkbd serio0: Unknown key pressed (translated set 2, code 0xa5 on isa0060/serio0).
8) [59169.780823] atkbd serio0: Use 'setkeycodes e025 <keycode>' to make it known.
9) [59169.783874] atkbd serio0: Unknown key released (translated set 2, code 0xa5 on isa0060/serio0).
10) [59169.783882] atkbd serio0: Use 'setkeycodes e025 <keycode>' to make it known.
There were 3 matches on lines:
1 2 3 

xieerqi:$ awk '/wlan0/{print "Match on " NR }' inputFile.txt
Match on 1
Match on 2
Match on 3

相关内容