One-liners are also used to show off the differential expressive power of
programming languages. Frequently, one-liners are used to demonstrate programming ability. Contests are often held to see who can create the most exceptional one-liner.
BASIC A single line of BASIC can typically hold up to 255 characters, and one liners ranged from simple games to graphical demos. One of the better-known demo one-liners is colloquially known as
10PRINT, written for the
Commodore 64: 10 PRINT CHR$(205.5+RND(1)); : GOTO 10
C The following example is a
C program (a winning entry in the "Best one-liner" category of the
IOCCC). main(int c,char**v){return!m(v[1],v[2]);}m(char*s,char*t){return*t-42?*s?63==*t|*s==*t&&m(s+1,t+1):!*t:m(s,t+1)||*s&&m(s+1,t);} This one-liner program is a glob pattern matcher. It understands the glob characters , meaning zero or more characters, and , meaning exactly one character, just like most
Unix shells. Run it with two args, the string and the glob pattern. The exit status is 0 (shell true) when the pattern matches, 1 otherwise. The glob pattern must match the whole string, so you may want to use * at the beginning and end of the pattern if you are looking for something in the middle. Examples: $ ./a.out foo 'f??'; echo $? $ ./a.out 'best short program' '??st*o**p?*'; echo $?
AWK The book
The AWK Programming Language contains 20 examples of
one-liners at the end of the book's first chapter. Here are the very first of them: • Print the total number of input lines (like
wc -l): END { print NR } • Print the tenth input line: NR == 10 • Print the last field of every input line: { print $NF }
J Here are examples in
J: • A function avg to return the average of a list of numbers: avg=: +/ % # •
Quicksort: quicksort=: (($:@(#[)) ({~ ?@#)) ^: (1
Perl Here are examples in the
Perl programming language: • Look for duplicate words perl -0777 -ne 'print "$.: doubled $_\n" while /\b(\w+)\b\s+\b\1\b/gi' • Find Palindromes in /usr/dict/words perl -lne 'print if $_ eq reverse' /usr/dict/words • in-place edit of *.c files changing all foo to bar perl -p -i.bak -e 's/\bfoo\b/bar/g' *.c Many one-liners are practical. For example, the following
Perl one-liner will reverse all the bytes in a file: perl -0777e 'print scalar reverse <>' filename While most
Perl one-liners are imperative, Perl's support for anonymous functions, closures, map, filter (
grep) and fold (List::Util::reduce) allows the creation of 'functional' one-liners. This one-liner creates a function that can be used to return a list of primes up to the value of the first parameter: my $z = sub { grep { $a=$_; !grep { !($a % $_) } (2..$_-1)} (2..$_[0]) } It can be used on the command line, like this: perl -e'$,=",";print sub { grep { $a=$_; !grep { !($a % $_) } (2..$_-1)} (2..$_[0]) }->(shift)' number to print out a comma-separated list of primes in the range 2 - number.
Haskell The following
Haskell program is a one-liner: it sorts its input lines
ASCIIbetically. main = (mapM_ putStrLn . Data.List.sort . lines) = An even shorter version: main = interact (unlines . Data.List.sort . lines) -- Ditto. Usable on the command line like: cat filename | ghc -e "interact (unlines . Data.List.sort . lines)"
Racket The following
Racket program is equivalent to the above Haskell example: • lang racket (for-each displayln (sort (port->lines) string and this can be used on the command line as follows: racket -e '(for-each displayln (sort (port->lines) string'
Python Performing one-liners directly on the Unix command line can be accomplished by using
Python's -cmd flag (-c for short), and typically requires the import of one or more modules. Statements are separated using ";" instead of newlines. For example, to print the last field of unix long listing: ls -l | python -c " import sys;[sys.stdout.write(' '.join([line.split(' ')[-1)) for line in sys.stdin]"
Python wrappers Several open-source scripts have been developed to facilitate the construction of Python one-liners. Scripts such as pyp or Pyline import commonly used modules and provide more human-readable variables in an attempt to make Python functionality more accessible on the command line. Here is a redo of the above example (printing the last field of a unix long listing): ls -l | pyp "whitespace[-1]" # "whitespace" represents each line split on white space in pyp ls -l | pyline "words[-1]" # "words" represents each line split on white space in pyline
Executable libraries The Python CGIHTTPServer module for example is also an executable library that performs as a web server with CGI. To start the web server enter: $ python -m CGIHTTPServer Serving HTTP on 0.0.0.0 port 8000 …
Tcl Tcl (Tool Command Language) is a dynamic programming/scripting language based on concepts of Lisp, C, and Unix shells. It can be used interactively, or by running scripts (programs) which can use a package system for structuring. Many strings are also well-formed lists. Every simple word is a list of length one, and elements of longer lists are separated by whitespace. For instance, a string that corresponds to a list of three elements: set example {foo bar grill} Strings with unbalanced quotes or braces, or non-space characters directly following closing braces, cannot be parsed as lists directly. You can explicitly split them to make a list. The "constructor" for lists is of course called list. It's recommended to use when elements come from variable or
command substitution (braces won't do that). As Tcl commands are lists anyway, the following is a full substitute for the list command: proc list args {set args}
Windows PowerShell Finding palindromes in file words.txt Get-Content words.txt | Where { $_ -eq -join $_[($_.length-1)..0] } Piping semantics in PowerShell help enable complex scenarios with one-liner programs. This one-liner in PowerShell script takes a list of names and counts from a comma-separated value file, and returns the sum of the counts for each name. ipcsv .\fruit.txt –H F, C|Group F|%{@{"$($_.Name)"=($_.Group|measure C -sum).Sum}}|sort value ==See also==