我正在调试一个嵌入式服务器,它将连续的一行文本输出到指定的网络端口。流中没有换行符,但是它是文本数据,我想在输出时格式化它。我尝试使用tr (转换)将流中的一个字符替换为一个能工作的换行符,但是不可能找到唯一的单个字符,它总是可以被换行符明智地替换。我最初的想法是使用sed向2-3个字符的模式中添加一个换行符,但是由于sed是基于行的,而且流是一个永不结束的单行,sed永远不会完成这个过程!是否有基于非线的sed替代方案?
发布于 2023-02-15 16:28:05
解算成
stdbuf -o0 ncat -ul 51002 | stdbuf -o0 fold | sed "s/\[15/\&\[15/g" -u | stdbuf -o0 tr -d '\n' | tr "&" "\n"15是我想加一个换行符的模式。有点变通,但效果很好。
发布于 2023-02-15 12:12:54
这就是fold的目的:
NAME
fold - wrap each input line to fit in specified width
SYNOPSIS
fold [OPTION]... [FILE]...
DESCRIPTION
Wrap input lines in each FILE, writing to standard output.
With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options
too.
-b, --bytes
count bytes rather than columns
-s, --spaces
break at spaces
-w, --width=WIDTH
use WIDTH columns instead of 80
--help display this help and exit
--version
output version information and exit正如您在上面看到的,它可以根据宽度折叠这一行,这样您就可以得到100个字符的行,其中包括:
command_that_reads_from_port | fold -w 100这是一个标准程序,是的一部分,因此它应该存在于任何GNU系统中。
https://unix.stackexchange.com/questions/735607
复制相似问题