1

I have a bash script that work fine with two awk statement, and I can visualize the outputs in the console for both statement but when I want to store the result in one file, I can get only one (it looks like a race sometimes the result of the statement 1 is stored sometimes the result of the statement 2). My code is like this

   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
Ben
  • 329
  • 1
  • 3
  • 17
  • 1
    possible duplicate of [How to append output to the end of text file in SHELL Script?](http://stackoverflow.com/questions/6207573/how-to-append-output-to-the-end-of-text-file-in-shell-script) – Kokozaurus Oct 10 '13 at 09:25

2 Answers2

1

Your second one needs to append the file by >>, not overwriting it by >

So:

awk 'this is first awk' > file.txt
awk 'this is second awk' >> file.txt
justhalf
  • 8,960
  • 3
  • 47
  • 74
1

To append the output of a bash command to an existing file use >>

#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
Francesco Montesano
  • 8,485
  • 2
  • 40
  • 64