2013-10-10 63 views
1

我有做工精細兩個AWK語句bash腳本,我可以想像在控制檯輸出兩種說法 但是當我想結果存儲在一個文件,我只能得到一個(它看起來像是一個種族,有時候語句1的結果有時候是語句2的結果)。 我的代碼是這樣的存儲在同一文件的多個AWK語句的結果

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 
+1

[如何在SHELL腳本中將輸出追加到文本文件的末尾?](http://stackoverflow.com/questions/6207573/how-to-append-output-to-the-end-of-text -file-in-shell-script) –

回答

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 
1

你的第二個需要給這個文件>>通過>

所以追加,而不是覆蓋它:

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

感謝您的幫助! – Ben

相關問題