One-liner program
Encyclopedia
A one-liner is textual input to the command-line of an operating system shell
Shell (computing)
A shell is a piece of software that provides an interface for users of an operating system which provides access to the services of a kernel. However, the term is also applied very loosely to applications and may include any software that is "built around" a particular component, such as web...

 that performs some function in just one line of input.

The one liner can be
  1. An expression written in the language of the shell.
  2. The invocation of an interpreter together with program source for the interpreter to run.
  3. The invocation of a compiler together with source to compile and instructions for executing the compiled program.


Certain dynamic
Dynamic programming language
Dynamic programming language is a term used broadly in computer science to describe a class of high-level programming languages that execute at runtime many common behaviors that other languages might perform during compilation, if at all...

 scripting
Scripting language
A scripting language, script language, or extension language is a programming language that allows control of one or more applications. "Scripts" are distinct from the core code of the application, as they are usually written in a different language and are often created or at least modified by the...

 languages such as AWK, sed, and Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

 have traditionally been adept at expressing one-liners.
Specialist shell
Shell (computing)
A shell is a piece of software that provides an interface for users of an operating system which provides access to the services of a kernel. However, the term is also applied very loosely to applications and may include any software that is "built around" a particular component, such as web...

 interpreters such as these Unix shell
Unix shell
A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems...

s or the Windows PowerShell
Windows PowerShell
Windows PowerShell is Microsoft's task automation framework, consisting of a command-line shell and associated scripting language built on top of, and integrated with the .NET Framework...

, allow for the construction of powerful one-liners.

The use of the phrase one-liner has been widened to also include program-source for any language that does something useful in one line.

History

The word One-liner has two references in the index of the book The AWK Programming Language (the book is often referred to by the abbreviation TAPL). It explains the programming language AWK, which is part of the Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 operating system
Operating system
An operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...

. The authors explain the birth of the One-liner paradigm with their daily work on early Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 machines:
Notice that this original definition of a One-liner implies immediate execution of the program without any compilation. So, in a strict sense, only source code for interpreted languages qualifies as a One-liner. But this strict understanding of a One-liner was broadened in 1985 when the IOCCC introduced the category of Best One Liner for C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

, which is a compiled language.

Examples

The TAPL book contains 20 examples of One-liners (A Handful of Useful awk One-Liners) at the end of the book's first chapter.

Here are the very first of them:

  1. Print the total number of input lines:
    END { print NR }

  2. Print the tenth input line:
    NR  10

  3. Print the last field of every input line:
    { print $NF }



Here are examples in J
J (programming language)
The J programming language, developed in the early 1990s by Kenneth E. Iverson and Roger Hui, is a synthesis of APL and the FP and FL function-level languages created by John Backus....

:


  1. A function avg to return the average of a list of numbers:
    avg=: +/ % #

  2. Quicksort:
    quicksort=: (($:@(<#[) , (=#[) , $:@(>#[)) ({~ ?@#)) ^: (1<#)



Here are examples in Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

:
  • 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
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

 one-liner will reverse all the bytes in a file:

perl -0777e 'print scalar reverse <>' filename

One-liners are also used to show off the differential expressive power of programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

s. Frequently, one-liners are used to demonstrate programming ability. Contests are often held to see who can create the most exceptional one-liner.

The following example is a C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 program (a winning entry in the "Best one-liner" category of the IOCCC, here split to two lines for presentation).


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 shell
Unix shell
A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems...

s.

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:

$ prog foo 'f??'; echo $?
$ prog 'best short program' '??st*o**p?*'; echo $?

Haskell

The following Haskell
Haskell (programming language)
Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...

 program is a one-liner: it sorts its input lines asciibetically.


main = (mapM_ putStrLn . List.sort . lines) =<< getContents -- In ghci a qualified name like List.sort will work, although as a standalone executable you'd need to import List.


An even shorter version:

main = interact (unlines . List.sort . lines) -- Ditto.

Racket

The following Racket program is equivalent to the above Haskell example:
  1. 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

Perl

While most Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

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.

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]"

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

It returns with:
Serving HTTP on 0.0.0.0 port 8000 ...

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:

ls -l | pyp "whitespace[-1]" #"whitespace" represents each line split on white space
ls -l | pyline "words[-1]" #"words" represents each line split on white space

External links
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK