I am still kind of new to programming, but willing to learn: I am struggling with a problem in following project: I am logging analog read-outs of an Arduino board. The Arduino board sends tab-separated values (as a string) via following command to the serial device on my machine.
loop {
[...]
Serial.println(stringOfTabSeparatedValues);
Serial.flush();
}
The values are than retrieved from the serial device and appended (logged) in file.log on my computer through following command:
ttylog -f -b 9600 -d /dev/ttyACM0 >> /dir/to/file.log
The content of file.log looks as following:
value1a value2a value3a value...
value1b value2b value3b value...
value1c value2c value3c value...
...
This seems to work so far. However, since I would like to link/load this file content to a spreadsheet which is updated in real time, I want to get rid of the empty lines inbetween. (Why does println() generate empty lines when read by ttylog in the first place?) I would like the lines to be logged to look like following:
value1a value2a value3a value...
value1b value2b value3b value...
value1c value2c value3c value...
...
I figured I could use a pipe to modify ttylog stdout before redirecting that modified output to the destination file. Here is what I have tried:
ttylog -f -b 9600 -d /dev/ttyACM0 | awk 'NF' >> /dir/to/file.log
ttylog -f -b 9600 -d /dev/ttyACM0 | awk 'NF' | tee -a /dir/to/file.log
ttylog -f -b 9600 -d /dev/ttyACM0 | sed -i '/^$/d' >> /dir/to/file.log
ttylog -f -b 9600 -d /dev/ttyACM0 | sed -i '/^$/d' | tee -a /dir/to/file.log
Unfortunately, in those cases nothing at all was written to file.log. (Why?) I noticed though, that following commands at least give my the desired output printing it to the terminal:
ttylog -f -b 9600 -d /dev/ttyACM0 | awk 'NF'
ttylog -f -b 9600 -d /dev/ttyACM0 | sed -i '/^$/d'
However I do not know how to redirect that output directly to a file. I figure my problem has something to do with both, awk and sed, normally using stdin and not stdout as input? Conclusively, I tried redirecting from ttylog to awk/sed and then to the destination file:
ttylog -f -b 9600 -d /dev/ttyACM0 >> awk 'NF' >> /dir/to/file.log
ttylog -f -b 9600 -d /dev/ttyACM0 >> sed -i '/^$/d' >> /dir/to/file.log
But here stdout was unmodified written to file.log. (Why?)
Is there a way to modify the output of ttylog (stdout) BEFORE redirecting that output to a file?
Cheers and thanks in advance.
ttylog -f -b 9600 -d /dev/ttyACM0 | awk 'NF {print; fflush()}' >> /dir/to/file.log
works like a charm!