-6

我正在使用一个在内部调用 awk 脚本的 shell 脚本程序……但我计划将整个脚本更改为 awk,因为我遇到了一些问题……我只想知道 shell 中有一些我会的命令想知道它们是否可以在 awk 中完成。

1) ls, cd, zcat .. 如果我能做到,我该怎么做.. 请大家对此提出一些建议。谢谢你。

4

2 回答 2

3

Shell commands and external utilities such as those you list can be performed by the AWK system() function. You may not need cd and ls depending on what you're doing. Something like zcat can be called with system() and the output read using getline.

cmd = "zcat filename"
while ((cmd | getline) > 0 {
    # do something with the data
}
close(cmd)
于 2012-07-18T01:19:20.790 回答
2

Welcome to StackOverflow. This is a programming resource. The questions that get the best answers tend to include source code, input data, expected results and actual results. You will get better answers if you TRY to do things, fail, then come here with specific questions.

That said, you can find out what commands are available within awk by running man awk from your shell. In general, you should keep in mind that ls and zcat and anything in /bin or /usr/bin are also not "built-in" to your shell, but are external programs that can be called from an interactive command line or a script in most languages. To be clear: ls and zcat are not part of awk, and are also not part of /bin/sh.

There are also different versions of awk, with different behaviours and different commands available. You will get the best documentation from your own system, since we don't know what tools you're using.

So ... for this question, show us your shell script, show us your attempt to make it an awk script, ask questions about specific problems you're having. Without seeing your code, we have no way of knowing whether what you want to do is even possible in shell, let alone awk.

于 2012-07-18T01:17:24.327 回答