Replace newline with literal \\n? - raku

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.

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.

Related

Raku operator for 2's complement arithmetic?

I sometimes use this:
$ perl -e "printf \"%d\", ((~18446744073709551592)+1)"
24
I can't seem to do it with Raku. The best I could get is:
$ raku -e "say +^18446744073709551592"
-18446744073709551593
So: how can I make Raku give me the same answer as Perl ?
Gotta go with (my variant¹ of) Liz's custom op (in her comment below).
sub prefix:<²^>(uint $a) { (+^ $a) + 1 }
say ²^ 18446744073709551592; # 24
My original "semi-educated wild guess"² that turned out to be acceptable to #zentrunix and the basis for Liz's op:
say (+^ my uint $ = 18446744073709551592) + 1; # 24
\o/ It works!³
Footnotes
¹ I flipped the two character op because I wanted to follow the +^ form, have it sub-vocalize as "two's complement", and avoid it looking like ^2.
² One line of thinking was about the particular integer. I saw that 18446744073709551592 is close to 2**64. Another was that integers are limited precision in Perl unless you do something to make them otherwise, whereas in Raku they are arbitrary precision unless you do something to make them otherwise. A third line of thinking came from reading the doc for prefix +^ which says "converts the number to binary using as many bytes as needed" which I interpreted as meaning that the representation is somehow important. Hmm. What if I try an int variable? Overflow. (Of course.) uint? Bingo.
³ I've no idea if this solution is right for the wrong reasons. Or even worse. One thing that's concerning is that uint in Raku is defined to correspond to the largest native unsigned integer size supported by the Raku compiler used to compile the Raku code. (Iirc.) In practice today this means Rakudo and whatever underlying platform is being targeted, and I think that almost certainly means C's uint64_t in almost all cases. I imagine perl has some similar platform dependent definition. So my solution, if it is a reasonable one, is presumably only portable to the degree that the Raku compiler (which in practice today means Rakudo) agrees with the perl binary (which in practice today means P5P's perl) when run on some platform. See also #p6steve's comment below.
'Long-hand' answer:
raku -e 'put ( (18446744073709551592.base(2) - 0b1).comb.map({!$_.Int+0}).join.parse-base(2));'
OR
raku -e 'say 18446744073709551592.base(2).comb.map({!$_.Int+0}).join.parse-base(2) + 1;'
Sample Output: 24
The answers above (should?) implement "Two's-Complement" encoding directly. Neither uses Raku's +^ twos-complement operator. The first one subtracts one from the binary representation, then inverts. The second one inverts first, then adds one. Neither answer feels truly correct, yet the same answer as Perl5 is obtained (24).
Looking at the Raku Docs page, one would conclude that the "twos-complement" of a positive number would be negative, hence it's not clear what the Perl (and now Raku) answers represent. Hopefully the foregoing is somewhat useful.
https://docs.raku.org/routine/+$CIRCUMFLEX_ACCENT

How to awk to read a dictionary and replace words in a file?

We have a source file ("source-A") that looks like this (if you see blue text, it comes from stackoverflow, not the text file):
The container of white spirit was made of aluminium.
We will use an aromatic method to analyse properties of white spirit.
No one drank white spirit at stag night.
Many people think that a potato crisp is savoury, but some would rather eat mashed potato.
...
more sentences
Each sentence in "source-A" is on its own line and terminates with a newline (\n)
We have a dictionary/conversion file ("converse-B") that looks like this:
aluminium<tab>aluminum
analyse<tab>analyze
white spirit<tab>mineral spirits
stag night<tab>bachelor party
savoury<tab>savory
potato crisp<tab>potato chip
mashed potato<tab>mashed potatoes
"converse-B" is a two column, tab delimited file.
Each equivalence map (term-on-left<tab>term-on-right) is on its own line and terminates with a newline (\n)
How to read "converse-B", and replace terms in "source-A" where a term in "converse-B" column-1 is replaced with the term in column-2, and then write to an output file ("output-C")?
For example, the "output-C" would look like this:
The container of mineral spirits was made of aluminum.
We will use an aromatic method to analyze properties of mineral spirits.
No one drank mineral spirits at bachelor party.
Many people think that a potato chip is savory, but some would rather eat mashed potatoes.
The tricky part is the term potato.
If a "simple" awk solution cannot handle a singular term (potato) and a plural term (potatoes), we'll use a manual substitution method. The awk solution can skip that use case.
In other words, an awk solution can stipulate that it only works for an unambiguous word or a term composed of space separated, unambiguous words.
An awk solution will get us to a 90% completion rate; we'll do the remaining 10% manually.
sed probably suits better since since it's only phrase/word replacements. Note that if the same words appear in multiple phrases first come first serve; so change your dictionary order accordingly.
$ sed -f <(sed -E 's_(.+)\t(.+)_s/\1/\2/g_' dict) content
The container of mineral spirits was made of aluminum.
We will use an aromatic method to analyze properties of mineral spirits.
No one drank mineral spirits at bachelor party.
Many people think that a potato chip is savory, but some would rather eat mashed potatoes.
...
more sentences
file substitute sed statement converts dictionary entries into sed expressions and the main sed uses them for the content replacements.
NB: Note that production quality script should take of word cases and also word boundaries to eliminate unwanted substring substitution, which are ignored here.

I need to figure out how to delimit a string in SQL

So, I'm at work atm and I had a co-worker create some SQL code for me to extract out text from a larger description field. The problem I'm running into is it doesn't stop extracting where I need it to. I need it to stop after it either sees the word "Specifications:" or when it finds two CRLF back to back. This would allow it to grab out only the "Features" which is what I'm trying for.
Here's an example of the current code:
SELECT IN_Desc, Replace(IN_Desc, Left(IN_Desc, InStr(IN_Desc, "- ") - 1), "")
FROM Inventory
WHERE IN_MfgName = "BERK"
Here's an example of the text it's looking through:
Gulp! has 400 times more scent dispersion than ordinary plastic bait.
The extreme scent dispersion greatly expands the strike zone allowing
you to catch more fish! Even more impressive, the natural formulation
of Gulp! out fishes live bait in head to head field tests. Berkley
Gulp! truly is the next generation in soft bait!
Features:
Ideal on jigs or as a trailer
Favorite for all SW species when targeting big fish
Proven tail action design swims under all conditions
Expand your strike zone with 400x more scent dispersion than plastic baits
15 years of Gulp! evolution…the best keeps getting better
Specifications:
Bait Length: 6"
Color: White
Quantity: Per 4
Packaging: Bag
Desired output:
Ideal on jigs or as a trailer
Favorite for all SW species when targeting big fish
Proven tail action design swims under all conditions
Expand your strike zone with 400x more scent dispersion than plastic baits
15 years of Gulp! evolution…the best keeps getting better
Thanks to everyone in advance for any and all help.
This is a bit ugly, but it seems to do the trick. It may need some tweaking to get exactly what you want, but this will get everything between Features and the next double carriagereturn/linefeed:
Mid(yourfield,InStr(1,yourfield, "Features:")+Len("Features: "),InStr(InStr(1,yourfield, "Features:")+Len("Features: "),yourfield, Chr(13) & Chr(10) & Chr(13) & Chr(10)))
I'm certain that it could be written prettier, but my access is rusty as hell. I feel like a VBA UDF would be a lot cleaner and then you could employ regex to really strip thing this apart.

group lines with a tag

Each written line belongs to the first tag upward.
For instance: ' The trucker is drunk ' belongs to AN8. I want to group all written lines belonging to the corresponding tag. Lines should remain in the same order.
input:
AN9
the cow is eating way too much
AN8
The trucker is drunk
AN9
The field are running out of herbs.
AN8
the truck is not going that staight
well of course the road is in curve
AN9
and
another line
AN8
The cop needs to check this out
AN9
now the cow is soooo big dude !
output:
AN9
the cow is eating way too much
The field are running out of herbs.
and
another line
now the cow is soooo big dude !
AN8
The trucker is drunk
the truck is not going that staight
well of course the road is in curve
The cop needs to check this out
Here is one awk
awk '/^AN/ {id=$0;next} {a[id]=a[id]"\n"$0} END {for (i in a) print i,a[i]}' file
AN8
The trucker is drunk
the truck is not going that staight
well of course the road is in curve
The cop needs to check this out
AN9
the cow is eating way too much
The field are running out of herbs.
and
another line
now the cow is soooo big dude !
If line starts with AN, use the info in the line as ID for array a.
Then store all lines in array a. Finally print all data in array a

LaTeX formal letter: signature align left

For the life of me, I can't seem to figure out how to fix this signature. Right now, it is right-hand justified, and I want to make it left-hand justified. I am still pretty green when it comes to LaTeX and formatting documents in it. It is probably something really simple.
Right now, I suspect the "\raggedright" command will have something to do with it. Just not sure exactly where.
\documentclass[10pt,stdletter,dateno]{newlfm}
\usepackage{kpfonts, sans}
\usepackage{url}
\title{title}
\widowpenalty=1000
\clubpenalty=1000
\newlfmP{headermarginskip=20pt}
\newlfmP{sigsize=50pt}
\newlfmP{dateskipafter=20pt}
\newlfmP{addrfromphone}
\newlfmP{addrfromemail}
\PhrPhone{Phone}
\PhrEmail{Email}
\namefrom{First \ Last}
\addrfrom{%
\today\\[10pt]
Who from \\
Street \\
City, State
}
\phonefrom{(123) 456-7890}
\emailfrom{email#mail.com}
\addrto{%
Division\\
Organization\\
Street\\
City, State}
\greetto{To Whom It May Concern,}
\closeline{Sincerely,}
\begin{document}
\begin{newlfm}
*Letter content*
\end{newlfm}
\end{document}
Bonus points if you know how to get rid of the black bars at the top and bottom, or reduce the header so that it is not taking up a huge amount of room.
Ugh... there's really no need to set any kind of letter using the letter or newlfm document class. As you have now come to notice, adjustments are not always all that easy. So I present an alternative, by just setting a letter as-is in the article document class:
\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{lipsum}% Just for this example
\setlength{\parindent}{0pt}% No paragraph indent
\pagestyle{empty}% No page headers/footers
\begin{document}
\hfill
\begin{tabular}{l#{}}
\today\\[10pt]
Who from \\
Street \\
City, State \\
Phone: (123) 456-7890 \\
Email: email#mail.com
\end{tabular}
\bigskip% or \vspace{20pt}
\begin{tabular}{#{}l}
Division\\
Organization\\
Street\\
City, State
\end{tabular}
\bigskip
To whom it may concern,
\bigskip
\lipsum[1]
\bigskip
Sincerely,
\bigskip\bigskip\bigskip
First~Last
\end{document}
In my opinion, the code is far more readable than using the letter or newlfm interface where some of the document content is scattered in the preamble and inside the document. Moreover, the newlfm.cls is void of any proper coding indentation, making debugging a painstaking task.
The geometry package is used to adjust the spacing in terms of the page layout.