raku.gg / one-liners

The -e Flag

2026-03-20

Raku shines as a command-line text processing tool. With the -e flag and its companions -n and -p, you can write powerful one-liners that rival Perl and awk while being more readable.

The -e Flag: Evaluate Code

The -e flag evaluates a string of Raku code directly from the command line:
raku -e 'say "Hello, World!"' raku -e 'say (1..10).sum' raku -e 'say 2 ** 64'
This is the starting point for all Raku one-liners. You pass a snippet of Raku code as an argument, and it runs immediately.

The -n Flag: Line-by-Line Processing

The -n flag wraps your code in an implicit loop that reads from STDIN (or files) line by line. Each line is available as $:
# Print each line with its length echo -e "hello\nworld\nfoo" | raku -ne 'say "$_: {.chars} chars"'
Output:
hello: 5 chars world: 5 chars foo: 3 chars
You can also pass filenames:
raku -ne 'say .uc' myfile.txt
This is equivalent to:
for lines() { say .uc }

The -p Flag: Print-by-Default

The -p flag works like -n but automatically prints $
after your code runs. This makes it perfect for transformations:
# Convert to uppercase echo -e "hello\nworld" | raku -pe '$_ = .uc' # Add line numbers echo -e "alpha\nbeta\ngamma" | raku -ne 'say "{++$}: $_"'
With -p, you modify $_ and it gets printed. No need to call say.

Combining Flags

Flags can be combined freely:
# -n with -e is the most common combo raku -ne 'say $_ if /error/i' logfile.txt # Multiple -e flags execute in sequence raku -e 'my $x = 42;' -e 'say $x * 2'

The -a Flag: Auto-split

Though Raku does not have a built-in -a flag like Perl, you can easily split lines:
# Split on whitespace, print second field echo -e "Alice 30\nBob 25\nCarol 35" | raku -ne 'say .words[1]' # Split on a delimiter echo -e "a:b:c\nd:e:f" | raku -ne 'say .split(":")[2]'

Useful Patterns

Filtering lines (grep-like)

raku -ne 'say $_ if /pattern/' file.txt raku -ne '.say if .starts-with("#")' file.txt raku -ne '.say if .chars > 80' file.txt

Counting matches

raku -e 'say lines().grep(/ERROR/).elems' logfile.txt

Summing numbers

echo -e "10\n20\n30" | raku -e 'say lines()>>.Int.sum'

Field extraction

# Print the first column of a space-delimited file raku -ne 'say .words[0]' data.txt # Print columns 2 and 4 raku -ne 'my @f = .words; say "@f[1] @f[3]"' data.txt

De-duplication

raku -e 'say $_ for lines().unique' file.txt

Frequency count

raku -e 'my %h; %h{$_}++ for lines(); .say for %h.sort(-*.value).head(10)' file.txt

BEGIN and END Blocks

You can use BEGIN and END phasers in one-liners just like in scripts:
# Print header and footer raku -ne 'BEGIN say "=== START ==="; say $_; END say "=== END ==="' file.txt

Reading From STDIN vs Files

Both -n and -p read from STDIN when no filename is given:
# From a pipe cat /etc/hosts | raku -ne 'say $_ if /localhost/' # From a file argument raku -ne 'say $_ if /localhost/' /etc/hosts # From multiple files raku -ne 'say $_ if /localhost/' file1.txt file2.txt

Quoting Tips

On Unix shells, single quotes are usually best for -e arguments since they prevent shell interpolation:
# Single quotes -- safe raku -e 'say "Hello, $*USER"' # If you need single quotes in your code, use double quotes outside raku -e "say 'Hello'" # Or use q// quoting raku -e 'say q[it'"'"'s fine]'

One-Liner vs Script

If your one-liner grows beyond about 100 characters, it is probably time to write a script instead. But for quick text processing, data extraction, and system administration tasks, Raku one-liners are hard to beat.

In upcoming posts, we will explore specific one-liner recipes for text processing, CSV handling, file renaming, and JSON manipulation.