awk 命令运行 C​​++ 代码并输入多次

标签 awk

比如说,我有一个 C 代码,我编译如下:

$ gcc code.c -o f.out
$ ./f.out inputfile outputfile

然后代码要求输入

$ enter mass:

现在,如果我需要运行此代码 200 次,并且输入文件的名称为:0c.txt、1c.txt、.....、199c.txt 等,并且我想每次使用相同的质量值时间(例如质量= 6)那么我如何为此编写“awk”命令?感谢您的帮助。

最佳答案

您没有指定输出文件名。我假设 0c.out, 1c.out, ...

我还假设 f.out 程序从 stdin 读取质量,而不是更复杂的内容。

#!/usr/bin/gawk -f
BEGIN {  
    mass = 6
    for (i=0; i<200; i++) {
        cmd = sprintf("./f.out %dc.txt %dc.out", i, i)
        print mass |& cmd
        close(cmd, "to")
        while ((cmd |& getline out) > 0) {
            do something with each line of output from ./f.out
        }
        close(cmd)
    }
}

引用http://www.gnu.org/software/gawk/manual/html_node/Two_002dway-I_002fO.html

在 bash 中,你可以这样写:

for i in $(seq 0 199); do
    echo 6 | ./f.out ${i}c.txt ${i}c.out
done

关于awk 命令运行 C​​++ 代码并输入多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26265152/

相关文章:

linux - Shebang 多个参数

unix - 根据两列选择唯一的行

bash - 在bash循环中减去 float

linux - 添加引号的字段分隔

linux - Awk:无法连接字符串

linux - Grep 来自多个文件的值

linux - 如何在 awk 中为每一行使用最大数量?

linux - 当列具有重复值时将后缀附加到列的 awk 脚本

python - 对每对字段执行计算

ubuntu - 当其他列值在 Ubuntu 中的引号内有逗号时,将一个列值复制到 CSV 中的另一个