1

我有一个 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
4

2 回答 2

1

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

所以:

awk 'this is first awk' > file.txt
awk 'this is second awk' >> file.txt
于 2013-10-10T09:25:00.037 回答
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
于 2013-10-10T09:25:16.020 回答