Questions tagged [one-liner]

One Liners are an abridged one-line version of a multi-line script.

Originally, a one-liner program was textual input to the command-line of an operating system shell that performs some function in just one line of input. The one-liner can be

  • an expression written in the language of the shell;
  • the invocation of an interpreter together with program source for the interpreter to run;
  • the invocation of a compiler together with source to compile and instructions for executing the compiled program.

Certain dynamic scripting languages such as AWK, sed, and Perl have traditionally been adept at expressing one-liners. Shell interpreters such as Unix shells or Windows PowerShell 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.

Find more information at:

153 questions
188
votes
24 answers

How do I concatenate multiple C++ strings on one line?

C# has a syntax feature where you can concatenate many data types together on 1 line. string s = new String(); s += "Hello world, " + myInt + niceToSeeYouString; s += someChar1 + interestingDecimal + someChar2; What would be the equivalent in C++?…
Nick Bolton
  • 38,276
  • 70
  • 174
  • 242
7
votes
3 answers

Perl/Raku succinct webserver one-liner?

Are there any concise one-liners for quick serving of pages or directories if no index.html? Something like this: python3 -m http.server Couldn't find a Raku one-liner. Compare Perl ones, taken from https://gist.github.com/willurd/5720255 and …
uxer
  • 521
  • 3
  • 10
6
votes
11 answers

What is a Unix oneliner to swap between fields?

I have a file with a list id1 str1 str2 .. strn id2 str1 str2 .. strm (the number of str can vary) and I want a oneliner that transforms it into str1 str2 .. strn [id] str1 str2 .. strm [id] There should be a way with awk to do that, but I don't…
kloop
  • 4,537
  • 13
  • 42
  • 66
5
votes
3 answers

How to convert between the unicode forms: string, name, number

I have been lately using unicode more often and wondered if there is a command line tool to convert unicode between its forms. Would be nice to be able to say: uni_convert "☃" --string And know that the string is defined in unicode as a "SNOWMAN".
Tim Potapov
  • 431
  • 2
  • 12
4
votes
1 answer

How does this discouraging Python one-liner that displays the mandlebrot set work?

Could you please explain how it works. In particular: where do a,c,n,s,z come from? And how to convert this to regular functions and their calls? x=10 y=5 abs( (lambda a: lambda z, c, n: a(a, z, c, n)) (lambda s, z, c, n: z if n == 0 else s(s,…
Ivar Wine
  • 43
  • 4
4
votes
3 answers

How to calculate sum of reciprocals with integers?

I would like to calculate the sum of reciprocals of a list of integers (and see if it is larger or equal to 1): I want to work with integers to avoid floating-point rounding issues. To do so, I want to work it out like this: I have done…
Rémi Baudoux
  • 542
  • 3
  • 16
4
votes
2 answers

How do I fill a new array with n distinct empty arrays, in a concise one-line initialization statement?

Here is where I am stuck. I want to take this statement and revise it in a manner that the empty array I fill (which I surmise might not work with dynamic values), will initialize bucket to the n distinct empty arrays. How do I do this? Is there…
Vahe
  • 1,699
  • 3
  • 25
  • 76
4
votes
2 answers

Why is this Perl one-liner of Fibonacci working?

print$f+=$z=$f-$z,$/for--$z..8 Alternatively, if you replace $z to $!, you can do the below. print$f+=$!=$f-$!for--$!..8 But why? $! is error perlval, isn't it?
eeeeeeeeengo
  • 133
  • 6
4
votes
1 answer

Why using utf8 patterns within perl substitute(s) and match(m) operators within one-liners does not work?

I found this issue when using Perl's one-liners for substituting some utf8 text in files. I am aware of hacks at How to handle utf8 on the command line (using Perl or Python)?. They don't work for this case. OS is linux, locate is set to utf8 # make…
okharch
  • 387
  • 2
  • 10
4
votes
2 answers

perl command line one liner to do find/replace, but *not* print lines that don't match?

I want to do a regex find & replace on a file on the command line with complicated regex, so I need PCRE's & am using perl -pe "s/foo/bar" (rather than sed), that prints out all lines after applying the s///, but it also prints lines that don't…
Amandasaurus
  • 58,203
  • 71
  • 188
  • 248
4
votes
3 answers

one liner to go through an iterable (generator)

I encountered some code looking like: [func(val) for val in iterable] There is an iterable (in my case a generator) for which a user wants to call a function for each value for its side effects (func could for example just be print) but where the…
gelonida
  • 5,327
  • 2
  • 23
  • 41
3
votes
3 answers

List comprehension instead of extend in loop

Can I write this code in one line? I tried use chain in list comprehension. def divisors(n): result = [] for div in range(1, int(sqrt(n)) + 1): if n % div == 0: result.extend([div, n / div]) return list(set(result))
Ashtart
  • 121
  • 5
3
votes
2 answers

One-liner extern "C" for a variable - legit?

Consider the following translation unit: extern "C" int x = 1; I know that C-mangling an int doesn't mean much; and that int x = 1 already guarantees external linkage, but this should work. Which it does; the thing is, GCC warns about using it,…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
3
votes
3 answers

Convert list with one item to item itself in dict value

For example, there is a dictionary with key-value pairs, where the values are lists with different "content". Some lists have only one element. These elements can be different types of data. Question: What is the most efficient way to convert list…
gremur
  • 1,645
  • 2
  • 7
  • 20
3
votes
6 answers

Simple one-liner to merge lines with common first field

In my work building an English language database, I often deal with text content from different sources, and need to merge lines that share the same first field. I often hack this in a text editor with a regex that captures a first field, searching…
Michael Douma
  • 1,144
  • 8
  • 21
1
2 3
10 11