awk命令的输出到一个文本文件

编程入门 行业动态 更新时间:2024-10-12 08:26:10
本文介绍了awk命令的输出到一个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道如何使用&GT简单的awk code的输出写入到一个文本文件;使用或在一个文本文件将其追加>>

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,

这是我的code,其输出需要写入一个文本文件中的行,

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

在这里 -F 用来定义一个字段分隔符,这是,,上述行的目的是搜索对于任何匹配的频谱使用费并打印第3,第4,第5,第6,第10和第11的价值,而第二个值的某些部分......这些值从USB设备读取的/ 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,

下面是该例子的结果,

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

问题是,输出不会停止到来,它是在像一个无限循环形式,我不得不preSS CTRL + C 终止它。

现在真正的问题,如何将此输出写入​​到一个文本文件?

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

它创建一个名为 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 ?

在情况下,如果不能与终端,是有可能用Python运行相同的命令和输出写入到一个文本文件,并终止了吗?

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 ?

推荐答案

您的输出可能是缓冲和您停止code之前,缓冲区将刷新。通过强制 AWK 只过程中的几个记录,它会正常退出,写缓冲输出。

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.

尝试强制 AWK 来处理它通过检查记录计数遇到的第一个十位。

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

的stty -F的/ dev / ttyUSB0的ispeed 4800&放大器;&安培; AWK -F,'/ SUF /和NR下; 10 {$打印3,$ 4 $ 5,$ 6,$ 10,$ 11 SUBSTR($ 2,1,2),。,substr($ 2,3,2),。,substr($ 2,5,2)} NR&GT; {9}退出'&LT;为/ dev / ttyUSB0&GT; outputfile.txt

NR 评估相对于第一条记录当前记录号读取时 AWK 启动。当 NR 大于9将退出。如果preFER仅打印 / SUF / 中的一个发生,你可以立即退出后打印。

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 / {$打印3,$ 4 $ 5,$ 6,$ 10,$ 11 SUBSTR($ 2,1,2),。,substr($ 2,3,2),。, SUBSTR($ 2,5,2);退出}'&LT;为/ dev / ttyUSB0&GT; outputfile.txt

更多推荐

awk命令的输出到一个文本文件

本文发布于:2023-11-24 06:02:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624179.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本文件   命令   awk

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!