用文字\\ n替换newline?

发布于 2025-02-07 02:03:20 字数 92 浏览 0 评论 0原文

我如何用raku中的文字'\ n'替换新线?我尝试了s/\ n/\\ n/,我期望工作但无效。

谢谢。

How would I replace a newline with a literal '\n' in raku? I tried s/\n/\\n/ which I expected to work but did not.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×纯※雪 2025-02-14 02:03:20

continue

If you read Shakespeare's Sonnet 18 into Raku slurp-wise, (a.k.a. all at once) this is what you'll get:

~$ 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"

Above you see embedded \n newlines when slurping (visualized with a call to .raku, giving you an idea how Raku represents objects internally). But if you read the file in line-wise, by default Raku one-liners implement newline processing identical to Perl's -l command-line flag. Which is to say, \n newlines are stripped from input, and added back during output (if so desired). So this is what you'll get when reading 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

Note above, \n newlines are removed (chomped) off of each line. A more readable output is obtained iterating via a for loop:

~$ 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."

Which is same as dropping for lines and changing command-line flag(s) from -e to -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."

So the first question I have is whether \n newlines actually exist in your Raku text object(s). If so, you can double-escape them with the code as follows (adding \ backslashes as required by your 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, if you don't have true \n newlines in your Raku text object(s), you can simply append them (or any other text--such as \\n). Use ~ tilde for string concatenation, and add \ backslashes as required by your 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.

Finally, I have to put this here just in case someone has the far-more-common, opposite issue: having to remove/correct \\n embedded characters from a text file. Using Raku:

~$ 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文