Text Processing One-Liners
2026-03-26
Raku's expressive syntax makes it a fantastic tool for quick text processing from the command line. In this post, we cover practical one-liners for filtering, transforming, counting, and reformatting text.
Filtering Lines (grep-like)
Match lines containing a pattern:
raku -ne '.say if /ERROR/' server.log
Case-insensitive match:
raku -ne '.say if m:i/warning/' server.log
Lines NOT matching a pattern:
raku -ne '.say unless /DEBUG/' server.log
Lines matching multiple patterns (AND):
raku -ne '.say if /ERROR/ && /database/' server.log
Lines matching any of several patterns (OR):
raku -ne '.say if /ERROR|WARN|FATAL/' server.log
Line Numbers
Print matching lines with line numbers:
raku -ne 'state $n = 0; $n++; say "$n: $_" if /TODO/' source.py
Or using the built-in .kv on lines:
raku -e 'for "file.txt".IO.lines.kv -> $n, $l { say "$n: $l" if $l ~~ /pattern/ }'
Extracting Fields
Print the second word of each line:
raku -ne 'say .words[1] // ""' data.txt
Print the last field:
raku -ne 'say .words[*-1]' data.txt
Print fields 2 through 4:
raku -ne 'say .words[1..3].join(" ")' data.txt
Split on a custom delimiter and extract:
raku -ne 'say .split(":")[0]' /etc/passwd
Transforming Text
Convert to uppercase:
raku -pe '$_ = .uc' file.txt
Convert to title case:
raku -pe '$_ = .tc' file.txt
Replace all occurrences of a word:
raku -pe '$_ = .subst("old", "new", :g)' file.txt
Strip leading and trailing whitespace:
raku -pe '$_ = .trim' file.txt
Remove blank lines:
raku -ne '.say if .trim.chars > 0' file.txt
Counting
Count lines in a file:
raku -e 'say lines().elems' file.txt
Count lines matching a pattern:
raku -e 'say lines().grep(/ERROR/).elems' server.log
Count words in a file:
raku -e 'say lines().map(*.words.elems).sum' file.txt
Count unique lines:
raku -e 'say lines().unique.elems' file.txt
Character frequency:
raku -e 'my %f; %f{$_}++ for lines().join.comb; .say for %f.sort(-*.value).head(10)' file.txt
Sorting
Sort lines alphabetically:
raku -e '.say for lines().sort' file.txt
Sort lines by length:
raku -e '.say for lines().sort(*.chars)' file.txt
Sort numerically (first field):
raku -e '.say for lines().sort(*.words[0].Int)' data.txt
Reverse sort:
raku -e '.say for lines().sort.reverse' file.txt
Deduplication
Remove duplicate lines (preserving order):
raku -e '.say for lines().unique' file.txt
Remove duplicate lines (sorted):
raku -e '.say for lines().sort.squish' file.txt
Show only duplicate lines:
raku -e 'my %s; for lines() { %s{$_}++; .say if %s{$_} == 2 }' file.txt
Column Alignment
Align columns with formatting:
raku -e 'for lines() { my @f = .words; printf "%-20s %10s\n", @f[0], @f[1] }' data.txt
Range Selection
Print lines 10 through 20:
raku -e '.say for lines()[9..19]' file.txt
Print the first 5 lines:
raku -e '.say for lines().head(5)' file.txt
Print the last 5 lines:
raku -e '.say for lines().tail(5)' file.txt
Print every other line:
raku -e 'for lines().kv -> $n, $l { $l.say if $n %% 2 }' file.txt
Joining and Splitting
Join all lines into one:
raku -e 'say lines().join(" ")' file.txt
Split a single line into multiple lines (on commas):
echo "a,b,c,d" | raku -ne '.split(",").map(*.say)'
Multi-file Processing
Search across multiple files:
raku -ne 'BEGIN my $f = ""; if $*ARGFILES.path ne $f { $f = $*ARGFILES.path }; say "$f: $_" if /ERROR/' *.log
Practical Recipes
Extract email addresses from text:
raku -ne '.comb(/<[a..z A..Z 0..9 . _ -]>+ "@" <[a..z A..Z 0..9 . -]>+/).map(*.say)' file.txt
Extract IP addresses:
raku -ne '.comb(/\d ** 1..3 "." \d ** 1..3 "." \d ** 1..3 "." \d ** 1..3/).map(*.say)' logfile
Wrap long lines at 80 characters:
raku -ne 'say .comb(80).join("\n")' file.txt
Number non-blank lines:
raku -ne 'state $n = 0; if .trim { $n++; say "$n\t$_" } else { .say }' file.txt
These one-liners show just a fraction of what Raku can do on the command line. The combination of powerful regex support, built-in list operations, and clean syntax makes Raku a joy for text processing.