awk command output to a text file

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 ?


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 ?


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


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?


  • 1º Day of Daylight Saving Time Java and JS showing a different behavior
  • Recursion Control flow
  • CKeditor stripping font tags instead of converting to span
  • Replace and retrieve placeholder value
  • Basic defensive programming [duplicate]
  • Scala using regex with or syntax in match case statement
  • Draw half infinite lines?
  • R sqldf renaming a field in a select statement
  • Available space left on drive - WinAPI - Windows CE
  • Create a link to a web page that runs a Javascript function on the page
  • Google Places API - Find a company's CID and LRD
  • Android cannot disable cut copy paste
  • In-place sed command not working
  • Eclipse MTJ doesn't see Java ME SDK 3.0 devices
  • blade.php method outputting it's result to the form
  • Hide HTML elements without javascript, only CSS
  • SAXReader not re-ecape characters
  • Appending Character to Character Array In C
  • How can I set a binding to a Combox in a UserControl?
  • How to have background script and something similar to a default popup?
  • Dynamically accessing properties of knockoutjs observable array
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Regex thinks I'm nesting, but I'm not
  • What is Eclipse's Declaration View used for?
  • How do I fake an specific browser client when using Java's Net library?
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Can I make an Android app that runs a web view in Chrome 39?
  • How can I estimate amount of memory left with calling System.gc()?
  • Jquery - Jquery Wysiwyg return html as a string
  • Apache 2.4 - remove | delete | uninstall
  • How to pass list parameters for each object using Spring MVC?
  • SVN: Merging two branches together
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Matrix multiplication with MKL
  • Hits per day in Google Big Query
  • File not found error Google Drive API
  • Append folder name and increment by 1 using batch script
  • Django query for large number of relationships
  • Converting MP3 duration time