将命令输出重定向到awk中的文件

时间:2014-07-11 18:44:10

标签: awk

  1. 我需要将awk中的特定命令的输出重定向到文件。
  2. 我需要访问if。
  3. 之外的数组

    我尝试过的事情:

    awk '
    {
      if (condition)
      {
        array[FNR]=$1;
        print array[FNR];
        df home/user/loc1 home/user/loc2 > file1.txt
      }
      fi
      print array[]
    }' /home/user/testfile.txt
    

    我得到的错误:

    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
    

1 个答案:

答案 0 :(得分:0)

我相信这就是你要找的东西,

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