将多个awk语句的结果存储在同一个文件中

时间:2013-10-10 09:22:29

标签: bash awk

我有一个bash脚本可以正常使用两个awk语句,我可以在控制台中为两个语句可视化输出 但是当我想将结果存储在一个文件中时,我只能获得一个(它看起来像一个种族,有时候语句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

2 个答案:

答案 0 :(得分:1)

您的第二个需要>>附加文件,而不是>

覆盖它

所以:

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

答案 1 :(得分: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