Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将命令输出重定向到awk中的文件 我只需要将awk中特定命令的输出重定向到文件。 我需要访问if外部的阵列。_Awk - Fatal编程技术网

将命令输出重定向到awk中的文件 我只需要将awk中特定命令的输出重定向到文件。 我需要访问if外部的阵列。

将命令输出重定向到awk中的文件 我只需要将awk中特定命令的输出重定向到文件。 我需要访问if外部的阵列。,awk,Awk,我所尝试的: 我得到的错误是: 我相信这就是你要找的 awk ' { if (condition) { array[FNR]=$1; print array[FNR] # >> file2.txt (use this if you want to print to a file) system("df home/user/loc1 home/user/loc2") >> file1.txt # Not sur

我所尝试的:

我得到的错误是:


我相信这就是你要找的

awk '
{
    if (condition)
    {
        array[FNR]=$1;
        print array[FNR] # >> file2.txt (use this if you want to print to a file)
        system("df home/user/loc1 home/user/loc2") >> file1.txt # Not sure why you want to run df shell command inside awk
    }
} END { # after the entire test file is read you can do your calculations below
    for (i in array) print array[i];
}' /home/user/testfile.txt

您似乎混合了shell和awk语法。例如,在awk中没有fi,在awk中您需要使用系统。。。发出shell命令。但是,从你的问题来看,你还没有100%清楚你想要实现什么。也许你可以添加一些更现实的细节。无论你想做什么,这都是完全错误的方法。请描述您想做什么,而不是如何使用示例输入和预期输出来获得更多帮助。@danfuzz不一定,您也可以使用getline发出命令。@Jidder确实如此,我为没有100%全面而道歉。我的主要观点是,上面的代码绝对不是如何发出shell命令。
awk: cmd. line:18:   df home/user/loc1 home/user/loc2 > file1.txt
awk: cmd. line:18:                                                     
                                                            ^ syntax error                                                                                                                                                    


awk: cmd. line:20:     print array[]
awk: cmd. line:20:                 ^ syntax error
awk: cmd. line:20: fatal: invalid subscript expression
awk '
{
    if (condition)
    {
        array[FNR]=$1;
        print array[FNR] # >> file2.txt (use this if you want to print to a file)
        system("df home/user/loc1 home/user/loc2") >> file1.txt # Not sure why you want to run df shell command inside awk
    }
} END { # after the entire test file is read you can do your calculations below
    for (i in array) print array[i];
}' /home/user/testfile.txt