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 }'`
echo $OUTPUT_FILE
? – Sparhawk Mar 05 '19 at 09:172>&1
– Romeo Ninov Mar 05 '19 at 09:19$OUTPUT_FILE
right now nothing gets printed out and the file itself is empty. – Owen Mar 05 '19 at 09:20sshpass -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-F
ontail
you will probably get the answer. This key keep the stream open and the redirection is buffered – Romeo Ninov Mar 05 '19 at 09:27gawk
instead ofawk
orawk -Winteractive
if theawk
on your system ismawk
(as on debian, ubuntu, etc). – Mar 05 '19 at 10:42tail -n1
instead oftail -F
it will work even without the command expansion and assignment to an extra variable. – Mar 05 '19 at 10:46