2

I know how to write the output of a simple awk code into a text file using > or append it in a text file using >>

but the issue here is different,

here is my line of code whose output needs to be written in a text file,

stty -F /dev/ttyUSB0 ispeed 4800 && awk -F"," '/SUF/ {print $3,$4,$5,$6,$10,$11,substr($2,1,2),".",substr($2,3,2),".",substr($2,5,2)}' < /dev/ttyUSB0

here -F is used to define a field seperator, which is ,, the above line aims to search for anything that matches SUF and prints its 3rd, 4th, 5th, 6th, 10th and 11th value , and some part of the 2nd value...these values are read from the USB device /dev/ttyUSB0

the above command is working absolutely fine and giving the correct output,

Here is the example output,

SUF Third Fourth Fifth Sixth Tenth Eleventh AA.BB.CC
SUF Third Fourth Fifth Sixth Tenth Eleventh AA.BB.CC
SUF Third Fourth Fifth Sixth Tenth Eleventh AA.BB.CC
SUF Third Fourth Fifth Sixth Tenth Eleventh AA.BB.CC
SUF Third Fourth Fifth Sixth Tenth Eleventh AA.BB.CC
SUF Third Fourth Fifth Sixth Tenth Eleventh AA.BB.CC
SUF Third Fourth Fifth Sixth Tenth Eleventh AA.BB.CC

The problem is, the output won't stop coming, and it is in a form of something like an infinite loop and I have to press CTRL+C to terminate it.

Now the real issue, How to write this output into a text file ?

Attempt:

I have tried this so far to write this output into a text file,

stty -F /dev/ttyUSB0 ispeed 4800 && awk -F"," '/SUF/ {print $3,$4,$5,$6,$10,$11,substr($2,1,2),".",substr($2,3,2),".",substr($2,5,2)}' < /dev/ttyUSB0 > outputfile.txt

it does create an output file called outputfile.txt, but it writes nothing inside it.

Which means I am not able to write the output in a text file at all. How am I supposed to write just first few lines of this output into a file text file ?

In case if isn't possible with terminal, is it possible to run the same command using Python and write the output to a text file and terminate it ?

Sufiyan Ghori
  • 18,164
  • 14
  • 82
  • 110
  • right now, i couldn't be able to write into a file at all as i said above. How am i suppose to control the number of lines when i haven't been able to sort out how to write this command o/p in a file ? – Sufiyan Ghori Dec 26 '13 at 17:45
  • When you run that command you said you tried does the output go to the screen? Because it looks like it should be going to the file so the question is where is it going if not there. – Etan Reisner Dec 26 '13 at 17:49
  • when i run the command without "> outputfile.txt" , the output display on the screen continuously (infinite loop)...but when I run it with "> outputfile.txt" then nothing happens, output goes no where. – Sufiyan Ghori Dec 26 '13 at 17:51
  • did this already, an empty outputfile.txt is created , nothing is written into a file. – Sufiyan Ghori Dec 26 '13 at 18:01
  • 1
    I see the similar problem here: http://stackoverflow.com/q/8058831/184968. Answer (http://stackoverflow.com/a/8058922/184968) says to add `fflush()` right in an awk script. –  Dec 26 '13 at 18:12
  • 1
    Or you can try answers to this question: http://unix.stackexchange.com/questions/25372/turn-off-buffering-in-pipe –  Dec 26 '13 at 18:17
  • fflush() does work :) but it added an extra 0 in the end of each line of the output..also i still have to terminate using CTRL+Z – Sufiyan Ghori Dec 26 '13 at 19:00
  • `. . . . awk '{. . . . substr($2,5,2)}' < /dev/ttyUSB0 | tail -20 > outputfile.txt` do anything for you? Good luck. – shellter Dec 26 '13 at 19:15
  • no luck with that too :S – Sufiyan Ghori Dec 26 '13 at 19:22
  • Can you force a timeout of the stty with time/min args as in [how-i-can-read-tty-file-with-timeout](http://stackoverflow.com/questions/6713668/how-i-can-read-tty-file-with-timeout)? If the timeout works, you might only have to disregard some incomplete output lines. – n0741337 Dec 27 '13 at 07:23
  • 1
    maybe change `tail` to `head` in my previous example? But using the NR counter in awk seems like the best approach. Good luck. – shellter Dec 27 '13 at 11:35

2 Answers2

3

Your output is probably buffered and you stop the code before the buffer is flushed. By forcing awk to only process several records, it will gracefully exit and write the buffered output.

Try forcing awk to process the first ten it encountered by checking the record count.

stty -F /dev/ttyUSB0 ispeed 4800 && awk -F"," '/SUF/ and NR < 10 {print $3,$4,$5,$6,$10,$11,substr($2,1,2),".",substr($2,3,2),".",substr($2,5,2)} NR > 9 {exit}' < /dev/ttyUSB0 > outputfile.txt

NR evaluates to the current record number relative to the first record read when awk started. When NR is greater than 9 it will exit. If you prefer to only print one occurrence of /SUF/ then you can immediately exit after the print.

stty -F /dev/ttyUSB0 ispeed 4800 && awk -F"," '/SUF/ {print $3,$4,$5,$6,$10,$11,substr($2,1,2),".",substr($2,3,2),".",substr($2,5,2); exit}' < /dev/ttyUSB0 > outputfile.txt

alvits
  • 6,550
  • 1
  • 28
  • 28
1

You're running awk on a file that is being continuously written to. At what point do you WANT awk to stop reading the file? Maybe what you want to do is something like this:

stty -F /dev/ttyUSB0 ispeed 4800 &&
cp /dev/ttyUSB0 tmp && 
awk -F"," '/SUF/ {print $3,$4,$5,$6,$10,$11,substr($2,1,2),".",substr($2,3,2),".",substr($2,5,2)}' tmp

so you take a snapshot of the file and awk runs on that?

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • i want my code to stop running after writing 10 outputs to a file , or even just 1 would do the job. your code aims to copy content from the file into a new file called "tmp" and then apply awk to it. but it is not doing what you're expecting it to be...it just copied the content from /dev/ttyUSB0 to tmp and didn't apply awk to it and I have to terminate the o/p using CTRL+C – Sufiyan Ghori Dec 26 '13 at 18:46
  • If you want awk to stop running after processing 10 lines, just add a line that says `NR==10{exit}` to it. – Ed Morton Dec 27 '13 at 01:49