Getting awk value and outputting to a file [duplicate]












1
















This question already has an answer here:




  • Turn off buffering in pipe

    13 answers



  • Flush 'tail -f' buffer?

    2 answers



  • Why does awk do full buffering when reading from a pipe

    3 answers




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 }'`









share|improve this question















marked as duplicate by Kusalananda awk
Users with the  awk badge can single-handedly close awk questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
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
















1
















This question already has an answer here:




  • Turn off buffering in pipe

    13 answers



  • Flush 'tail -f' buffer?

    2 answers



  • Why does awk do full buffering when reading from a pipe

    3 answers




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 }'`









share|improve this question















marked as duplicate by Kusalananda awk
Users with the  awk badge can single-handedly close awk questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
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














1












1








1









This question already has an answer here:




  • Turn off buffering in pipe

    13 answers



  • Flush 'tail -f' buffer?

    2 answers



  • Why does awk do full buffering when reading from a pipe

    3 answers




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 }'`









share|improve this question

















This question already has an answer here:




  • Turn off buffering in pipe

    13 answers



  • Flush 'tail -f' buffer?

    2 answers



  • Why does awk do full buffering when reading from a pipe

    3 answers




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 }'`




This question already has an answer here:




  • Turn off buffering in pipe

    13 answers



  • Flush 'tail -f' buffer?

    2 answers



  • Why does awk do full buffering when reading from a pipe

    3 answers








awk pipe cat






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 5 at 10:42







Owen

















asked Mar 5 at 9:14









OwenOwen

65




65




marked as duplicate by Kusalananda awk
Users with the  awk badge can single-handedly close awk questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
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.









marked as duplicate by Kusalananda awk
Users with the  awk badge can single-handedly close awk questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
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



















  • 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

















What is the output of echo $OUTPUT_FILE?

– Sparhawk
Mar 5 at 9:17





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





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







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




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





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




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





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










0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

is 'sed' thread safe

How to make a Squid Proxy server?