0

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 }'`
Owen
  • 9
  • What is the output of echo $OUTPUT_FILE? – Sparhawk Mar 05 '19 at 09:17
  • Try to add on the end of line 2>&1 – Romeo Ninov Mar 05 '19 at 09:19
  • If I do echo $OUTPUT_FILE right now nothing gets printed out and the file itself is empty. – Owen Mar 05 '19 at 09:20
  • I tried putting 2>&1 at the end of the line like so sshpass -p $PASSWORD ssh $USERNAME@$REMOTE_IP_ADDR tail -F $INPUT_FILE | awk '{ print $16 }' > $OUTPUT_FILE 2>&1 and still nothing. – Owen Mar 05 '19 at 09:23
  • 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 05 '19 at 09: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). –  Mar 05 '19 at 10:42
  • if you use tail -n1 instead of tail -F it will work even without the command expansion and assignment to an extra variable. –  Mar 05 '19 at 10:46
  • Ah okay , good to know. Thanks for the info @mosvy – Owen Mar 05 '19 at 10:48

0 Answers0