用文字\n替换换行符?



如何将换行符替换为raku中的文字'n'?我尝试了s/n/\n/,我希望工作,但没有。

谢谢。

如果你把莎士比亚的十四行诗第18首读成Rakuslurp-wise,(也就是一次读完)你会得到:

~$ raku -e 'slurp.raku.put;' sonnet18.txt
"Shall I compare thee to a summer’s day?nThou art more lovely and more temperate:nRough winds do shake the darling buds of May,nAnd summer’s lease hath all too short a date;nSometime too hot the eye of heaven shines,nAnd often is his gold complexion dimm'd;nAnd every fair from fair sometime declines,nBy chance or nature’s changing course untrimm'd;nBut thy eternal summer shall not fade,nNor lose possession of that fair thou ow’st;nNor shall death brag thou wander’st in his shade,nWhen in eternal lines to time thou grow’st:n   So long as men can breathe or eyes can see,n   So long lives this, and this gives life to thee.n"

上面你可以看到slurp调用时嵌入的n换行符(通过对.raku的调用可视化,让你了解Raku如何在内部表示对象)。但是,如果您以line方式阅读该文件,默认情况下,Raku一行程序实现的换行处理与Perl的-l命令行标志相同。也就是说,从输入中删除n换行符,并在输出期间添加回来(如果需要的话)。这就是你在阅读line-wise:

时得到的结果
~$ raku -e 'lines.raku.put;' sonnet18.txt
("Shall I compare thee to a summer’s day?", "Thou art more lovely and more temperate:", "Rough winds do shake the darling buds of May,", "And summer’s lease hath all too short a date;", "Sometime too hot the eye of heaven shines,", "And often is his gold complexion dimm'd;", "And every fair from fair sometime declines,", "By chance or nature’s changing course untrimm'd;", "But thy eternal summer shall not fade,", "Nor lose possession of that fair thou ow’st;", "Nor shall death brag thou wander’st in his shade,", "When in eternal lines to time thou grow’st:", "   So long as men can breathe or eyes can see,", "   So long lives this, and this gives life to thee.").Seq

上面注意,每行的n换行被删除(chomp)。通过for循环迭代获得更可读的输出:

~$ raku -e '.raku.put for lines;' sonnet18.txt
"Shall I compare thee to a summer’s day?"
"Thou art more lovely and more temperate:"
"Rough winds do shake the darling buds of May,"
"And summer’s lease hath all too short a date;"
"Sometime too hot the eye of heaven shines,"
"And often is his gold complexion dimm'd;"
"And every fair from fair sometime declines,"
"By chance or nature’s changing course untrimm'd;"
"But thy eternal summer shall not fade,"
"Nor lose possession of that fair thou ow’st;"
"Nor shall death brag thou wander’st in his shade,"
"When in eternal lines to time thou grow’st:"
"   So long as men can breathe or eyes can see,"
"   So long lives this, and this gives life to thee."

这与删除for lines并将命令行标志从-e更改为-ne相同:

~$ raku -ne '.raku.put;' sonnet18.txt
"Shall I compare thee to a summer’s day?"
"Thou art more lovely and more temperate:"
"Rough winds do shake the darling buds of May,"
"And summer’s lease hath all too short a date;"
"Sometime too hot the eye of heaven shines,"
"And often is his gold complexion dimm'd;"
"And every fair from fair sometime declines,"
"By chance or nature’s changing course untrimm'd;"
"But thy eternal summer shall not fade,"
"Nor lose possession of that fair thou ow’st;"
"Nor shall death brag thou wander’st in his shade,"
"When in eternal lines to time thou grow’st:"
"   So long as men can breathe or eyes can see,"
"   So long lives this, and this gives life to thee."

所以我的第一个问题是n换行符是否真的存在于你的Raku文本对象中。如果是这样,您可以使用以下代码对它们进行双转义(根据shell的要求添加反斜杠):

~$ raku -e 'put S:g/n/\\n/ given slurp;' sonnet18.txt
Shall I compare thee to a summer’s day?\nThou art more lovely and more temperate:\nRough winds do shake the darling buds of May,\nAnd summer’s lease hath all too short a date;\nSometime too hot the eye of heaven shines,\nAnd often is his gold complexion dimm'd;\nAnd every fair from fair sometime declines,\nBy chance or nature’s changing course untrimm'd;\nBut thy eternal summer shall not fade,\nNor lose possession of that fair thou ow’st;\nNor shall death brag thou wander’st in his shade,\nWhen in eternal lines to time thou grow’st:\n   So long as men can breathe or eyes can see,\n   So long lives this, and this gives life to thee.\n

OTOH,如果你的Raku文本对象中没有真正的n换行符,你可以简单地附加它们(或任何其他文本-如\n)。使用~波浪进行字符串连接,并根据shell的要求添加反斜杠:

~$ raku -e 'lines.map(* ~ "\\n").join.put;' sonnet18.txt
Shall I compare thee to a summer’s day?\nThou art more lovely and more temperate:\nRough winds do shake the darling buds of May,\nAnd summer’s lease hath all too short a date;\nSometime too hot the eye of heaven shines,\nAnd often is his gold complexion dimm'd;\nAnd every fair from fair sometime declines,\nBy chance or nature’s changing course untrimm'd;\nBut thy eternal summer shall not fade,\nNor lose possession of that fair thou ow’st;\nNor shall death brag thou wander’st in his shade,\nWhen in eternal lines to time thou grow’st:\n   So long as men can breathe or eyes can see,\n   So long lives this, and this gives life to thee.\n
#OR (below gives same result as above--but without final \n):
$ raku -e 'lines.join("\\n").put;' sonnet18.txt
Shall I compare thee to a summer’s day?\nThou art more lovely and more temperate:\nRough winds do shake the darling buds of May,\nAnd summer’s lease hath all too short a date;\nSometime too hot the eye of heaven shines,\nAnd often is his gold complexion dimm'd;\nAnd every fair from fair sometime declines,\nBy chance or nature’s changing course untrimm'd;\nBut thy eternal summer shall not fade,\nNor lose possession of that fair thou ow’st;\nNor shall death brag thou wander’st in his shade,\nWhen in eternal lines to time thou grow’st:\n   So long as men can breathe or eyes can see,\n   So long lives this, and this gives life to thee.

最后,我必须把这个放在这里,以防有人有更常见的,相反的问题:必须从文本文件中删除/纠正\n嵌入字符。使用乐烧:

~$ cat double_esc18.txt
Shall I compare thee to a summer’s day?\nThou art more lovely and more temperate:\nRough winds do shake the darling buds of May,\nAnd summer’s lease hath all too short a date;\nSometime too hot the eye of heaven shines,\nAnd often is his gold complexion dimm'd;\nAnd every fair from fair sometime declines,\nBy chance or nature’s changing course untrimm'd;\nBut thy eternal summer shall not fade,\nNor lose possession of that fair thou ow’st;\nNor shall death brag thou wander’st in his shade,\nWhen in eternal lines to time thou grow’st:\n   So long as men can breathe or eyes can see,\n   So long lives this, and this gives life to thee.\n
~$ raku -pe 's:g/\\n/n/;' double_esc18.txt
Shall I compare thee to a summer’s day?
Thou art more lovely and more temperate:
Rough winds do shake the darling buds of May,
And summer’s lease hath all too short a date;
Sometime too hot the eye of heaven shines,
And often is his gold complexion dimm'd;
And every fair from fair sometime declines,
By chance or nature’s changing course untrimm'd;
But thy eternal summer shall not fade,
Nor lose possession of that fair thou ow’st;
Nor shall death brag thou wander’st in his shade,
When in eternal lines to time thou grow’st:
So long as men can breathe or eyes can see,
So long lives this, and this gives life to thee.

最新更新