If you read Shakespeare’s Sonnet 18 into Raku slurp
-wise, (aka 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 slurp
ing (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 (chomp
ed) off of each line. A more readable output is obtained iterating using 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 the same as dropping for lines
and changing the command-line flag 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.