This question already has an answer here:

I need to get a value from a log file which I am getting using awk at the moment. I can get the number printing on the console but when I try to put that value into a new file nothing happens. Here is what i have

 sshpass -p $PASSWORD ssh $USERNAME@$REMOTE_IP_ADDR  tail -F $INPUT_FILE |  awk '{ print $16 }' > $OUTPUT_FILE

//when command is run without > $OUTPUT_FILE i get my value eg 4000

This isnt a buffering issue because there is nothing being outputted to the file. A file gets created but the value isnt written to the new file. Also my value is always being overwritten as it is reading a line from another script which continuously replaces the line I read.

So i have fixed the problem I was having. The solution was assigning the command to a variable and the writing that variable to a file. I tried this before but the issue I had was i had an extra space between my = sign and the command. Here is the working code

 value=`sshpass -p $PASSWORD ssh $USERNAME@$REMOTE_IP_ADDR tail -n 1 $INPUT_FILE | awk '{ print $16 }'`

        echo $value

        echo $value > $OUTPUT_FILE

//will not work because of the space between "value= `sshpass"
 //  value= `sshpass -p $PASSWORD ssh $USERNAME@$REMOTE_IP_ADDR tail -n 1 $INPUT_FILE | awk '{ print $16 }'`

marked as duplicate by Kusalananda awk Mar 5 at 9:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • What is the output of echo $OUTPUT_FILE? – Sparhawk Mar 5 at 9:17
  • Try to add on the end of line 2>&1 – Romeo Ninov Mar 5 at 9:19
  • If I do echo $OUTPUT_FILE right now nothing gets printed out and the file itself is empty. – Owen Mar 5 at 9:20
  • 1
    If you try w/o -F on tail you will probably get the answer. This key keep the stream open and the redirection is buffered – Romeo Ninov Mar 5 at 9:27
  • 1
    yes that is a buffering issue, not on the output of awk but on its input; I suggest you try gawk instead of awk or awk -Winteractive if the awk on your system is mawk (as on debian, ubuntu, etc). – mosvy Mar 5 at 10:42

Browse other questions tagged or ask your own question.