Using Raku (formerly known as Perl_6)
~$ raku -e 'put join ",\n", lines.map: *.subst(:global, /^ | $/, "\"");' file
"ANONYMOUS",
"APEX_040200",
"APEX_PUBLIC_USER",
"APPQOSSYS",
"AUDSYS"
OR:
~$ raku -e 'put join Q:b[,\n], lines.map: *.subst(:global, /^ | $/, Q:b["]);' file
"ANONYMOUS",
"APEX_040200",
"APEX_PUBLIC_USER",
"APPQOSSYS",
"AUDSYS"
Above, both the zero-width beginning-of-string ^
token and the zero-width end-of-string $
token are subst
-ituted with a double-quote character. Adding the :global
named-argument assures that both substitutions per line are performed. The lines
are then join
-ed on ",\n"
comma-newline to give the final output. (FYI, just join
on ","
comma if you want all your lines concatenated into one and separated by commas).
For quoting problems Raku offers an entire sub-language called the Q-lang to help massage your text into its final format. Hence the Q:b["]
"quote-respecting-backslash-interpolation" code above. The Q:b[...]
format works great for inserting characters like Q:b[\t]
tabs and Q:b[\n]
newlines without having to add extraneous single/double- quotes.
Specifically for single-quote and double-quote problems... Raku offers a related solution. Just use the Unicode name (either \c[APOSTROPHE]
or \c[QUOTATION MARK]
) as below. The fewer single/double- quotes in your one-liners the more portable your code is (e.g. to Windows):
~$ raku -e 'put join Q:b[,\n], lines.map: *.subst(:global, /^ | $/, "\c[APOSTROPHE]");' file
'ANONYMOUS',
'APEX_040200',
'APEX_PUBLIC_USER',
'APPQOSSYS',
'AUDSYS'
#OR:
~$ raku -e 'put join Q:b[,\n], lines.map: *.subst( /^ | $/, Q:b[\c[APOSTROPHE]]);' file
'ANONYMOUS',
'APEX_040200',
'APEX_PUBLIC_USER',
'APPQOSSYS',
'AUDSYS'
[Below I try to translate @terdon's excellent Perl solutions into Raku. But hopefully the above suffices].
Raku only has eof
as method call on filehandles. So an approximate (but verbose) translation of the Perl solution by @terdon is as follows:
~$ raku -e 'my $fh = $*ARGFILES.IO.open; for $fh.lines() {
put $fh.eof ?? (Q:b["] ~ $_ ~ Q:b["]) !! (Q:b["] ~ $_ ~ Q:b[",])
};' file
#OR:
~$ raku -e 'my $fh = $*ARGFILES.IO.open; put $fh.eof
?? (Q:b["] ~ $_ ~ Q:b["])
!! (Q:b["] ~ $_ ~ Q:b[",])
for $fh.lines();' file
#OR:
~$ raku -e 'given $*ARGFILES.IO.open -> $fh { $fh.eof
?? put(""" ~ $_ ~ """)
!! put(""" ~ $_ ~ "",")
for $fh.lines() } ;' file
In Raku, the ternary operator is Test ??
True !!
False , so the code scans pretty closely with the ternary Perl answer given by @terdon. Also, string-concatenation in Raku is accomplished with ~
tilde (commas work just as well). Regexes are discarded in this solution and indeed the Raku code could be more simply written with backslash escapes as put("\"$_\"")
or similar.
@terdon provides a -007
Perl solution which reads the entire file into memory. Raku's equivalent is slurp
ing the file. Below zero-width ^^
beginning-of-line and $$
end-of-line regex tokens are utilized:
~$ raku -e 'given slurp() {S:g/ ^^ /"/ andthen S:g/ $$ /",/ andthen S/ \,\n $ // andthen .put};' file
Probably obvious but notable anyway: save code inside the singlequotes to something like doublequote.p6
if you want a zero-boilerplate script you can run (+/- making it executable).
Sample Input:
ANONYMOUS
APEX_040200
APEX_PUBLIC_USER
APPQOSSYS
AUDSYS
https://docs.raku.org/syntax/eof%20-%20perlfunc
https://raku.org