你不能自己得到tr
它。tr
非常方便,但严格来说是逐字符过滤器,没有前瞻或后视。
您也许可以使用 获得示例输出sed
,但这真的很痛苦(我认为!)。编辑(sed 大师 @Sorpigal 证明我错了!)
这是一个解决方案awk
/home/shellter:>cat <<-EOS \
| awk 'BEGIN{RS="\n\n"}; { gsub("\n", "", $0) ;printf("%s %s", $0, "\n\n") }'
The answer t
o your question
A conclusive a
nswer isn’t al
ways possible.
When in doubt, ask pe
ople to cite their so
urces, or to explain
Even if we don’t agre
e with you, or tell y
ou.
EOS
# output
The answer to your question
A conclusive answer isnt always possible.
When in doubt, ask people to cite their sources, or to explain
Even if we dont agree with you, or tell you.
奇怪的是,它显示为三倍行距,但实际上是 dbl 行距的。
awk 具有为每个文件填充的预定义变量,以及它读取的每一行文本,即
RS = RecordSeperator -- normally a line of data, but a configurable value, that when set
to '\n\n' means a blank line, or a typical separation on a paragraph
$0 = complete line of text (as defined by the internal variables RS (RecordSeparator)
In this problem, it is each paragraph of data, viewed though
as a record.
$1 = first field in text (as defined by the internal variables FS (FieldSeparator)
which defaults to (possibly multiple) space chars OR tab char
a line with 2 connected spaces chars and 1 tab char has 3 fields)
NF = Number(of)Fields in current line of data (again fields defined by value of FS as
described above)
(there are many others, besides, $0, $n, $NF, $FS, $RS).
您可以通过使用示例代码中的变量以编程方式递增 $1、$2、$3 等值,例如 $i(i 是一个数字介于 2 和 NF 之间的变量。前导“$”表示给我值字段 i(即 $2、$3、$4 ...)
我希望这有帮助。