【问题标题】:store the result of multiple awk statement in the same file将多个 awk 语句的结果存储在同一个文件中
【发布时间】:2013-10-10 17:25:16
【问题描述】:

我有一个可以与两个 awk 语句一起正常工作的 bash 脚本,并且我可以在控制台中可视化这两个语句的输出 但是当我想将结果存储在一个文件中时,我只能得到一个(看起来像是一场比赛,有时语句 1 的结果存储有时语句 2 的结果)。 我的代码是这样的

   awk -F "," '

   BEGIN {

   print" ===================================================================== "
                          {printf "%80s\n",  "Table 1"  } 

  print"======================================================================= "


    }

 ##process table 1

 END {

 print " #######  END TABLE 1 ##################\n\n\n "
 } ' >file.txt

 ###### 2nd statement 

   awk -F "," '

   BEGIN {

   print" ====================================================== "
                    {printf "%80s\n",  "Table 2"  }                                                                       
 print"========================================================== "

 }

 ##process table 2

 END {

 print "################END TABLE 2 ######################3 \n\n\n "
 } ' >file.txt

【问题讨论】:

标签: bash awk


【解决方案1】:

要将 bash 命令的输出附加到现有文件,请使用 >>

#each time create a new file.txt
echo test1 > file.txt
echo test2 > file.txt

#if file.txt does not exists, behave like >, otherwise append to it
echo test3 >> file.txt

more file.txt
>> test2
   test3

【讨论】:

    【解决方案2】:

    您的第二个需要通过>> 附加文件,而不是通过> 覆盖它

    所以:

    awk 'this is first awk' > file.txt
    awk 'this is second awk' >> file.txt
    

    【讨论】:

    • 感谢您的帮助!!
    猜你喜欢
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多