Raku Books / Raku One-Liners / Command-Line Options
Examples of short one-lines
To warm up, let’s start with a few simple one-liners for working with files. (There’s also the whole Chapter 2 which is about working with files).
Double-space a file
$ raku -npe's/$/\n/' text.txtRemove all blank lines
$ raku -ne'.say if .chars' text.txtDepending on how you define ‘blank’, you may want another one-liner that skips the lines containing whitespaces:
$ raku -ne'.say if /\S/' text.txtNumber all lines in a file
$ raku -ne'say ++$ ~ ". " ~ $_' text.txtThis code, probably, requires a comment. The $ variable
is a state variable and it can be used without declaration.
Convert all text to uppercase
$ raku -npe'.=uc' text.txtStrip whitespace from the beginning and end of each line
$ raku -npe'.=trim' text.txtPrint the first line of a file
$ raku -ne'.say ; exit' text.txtPrint the first 10 lines of a file
$ raku -npe'exit if $++ == 10' text.txtThis time, the postfix ++ operator was applied to the
$ variable.
Course navigation
← Using command-line options | Reading files with $*ARGFILES →