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
1
vote
2 answers

How do I expand on this one line Perl script to run a command until specific KEYWORD in output, then also play a sound?

I have a problem with my NordVPN account where I have to sometimes run the client upwards of 10 times, with their terrible CLI tool, until it finally successfully chooses a server that accepts my login. I came across an old post here which I adapted…
1
vote
4 answers

How can I create a file and make it executable without typing the filename twice?

I would like to be able to chmod +x and touch a file in one go without having to list the file name twice as in touch foo && chmod +x foo. Is there a way to do this is in one whack? Here is some pseudo-code explanation describing the expected…
qq4
  • 282
  • 4
  • 14
1
vote
2 answers

Perl regex match string including multiple newlines

How do I match a multiple line string in a file like # custom prompt aa=`command1 arg1 arg2` bb=`command2 arg3 arg4` PS1="$aa$bb" # custom prompt I am using this perl -0pe 's/# custom prompt\n.*\n.*\n.*\n# custom prompt\n//gm' -i .bashrc I want to…
Jihyun
  • 883
  • 5
  • 17
1
vote
3 answers

How to have one liner code for creating and updating map entries?

In C++, when using maps, one of the ways we can quickly create a new entry and update a map at the same time is by simply doing something like: dict[key]+=1; This allows you to bypass checking to see if a particular key already exists, and then…
FIRES_ICE
  • 113
  • 5
1
vote
1 answer

Printing out filenames of txt.-files that include at least one line of words, each starting with vowels. Only 1 line of code

My task is to write only one line of code in Python. The code should do the following: I need to print out filenames of txt.files, which include at least one line of words, starting with only vowels (each word needs to start with a vowel from at…
alxnom334
  • 33
  • 4
1
vote
1 answer

Issue matching Chinese characters in Perl one liner using \p{script=Han}

I'm really stumped by trying to match Chinese characters using a Perl one liner in zsh. I canot get \p{script=Han} to match Chinese characters, but \P{script=Han} does. Task: I need to change this: 一 二 to this:
aewan86
  • 35
  • 4
1
vote
2 answers

What's the most efficient way to check multiple hash references in perl

I have a multidimensional data structure for tracking different characteristics of files I am comparing and merging data for. The structure is set up as such: $cumulative{$slice} = { DATA => $data, META => $got_meta, RECOVER =>…
WetCheerios
  • 155
  • 7
1
vote
3 answers

C consecutive(not nested) one-liner for loops not iterating properly

Final Edit When debugging this code, I would see the first loop line execute n times, but the following lines would execute only once. Only later I would notice that the expected value changes, though, were in fact being made: it was just the loop…
101is5
  • 309
  • 2
  • 12
1
vote
2 answers

perl -lane change delimiter

I have one liner: az account list -o table |perl -lane 'print $F[0] if /GS/i' I want to change default delimiter from '\t' to '-' Any hint how to do this? Just wanted to stress that it is oneliner I look for ;)
eswues
  • 119
  • 1
  • 10
1
vote
1 answer

Perl Split Function - How to assign a specify array value to a variable in one line

Is this a way of using a one-liner to assign an array value to a variable using the split function. I have the following code. This works. my @Fields = split(',',@_[0]); my $Unit = @Fields[2]; Wondering if it could be written in one like the…
1
vote
2 answers

How to rewrite this function in one line?

I have the following lines of code: f((k,v)) = Symbol(k) => Symbol(v) Dict(Iterators.map(f, pairs(names))) And I want to write it in a single line. I tried this: Dict(Iterators.map((k,v) -> Symbol(k) => Symbol(v), pairs(names))) But it throws…
Jafar Isbarov
  • 1,363
  • 1
  • 8
  • 26
1
vote
2 answers

List iteration with substitution into one-liner

I want to turn the following for loop: n = 10 x0 = 0 values = [x0] for i in range(n): x0 = f(x0) values.append(x0) into a one liner. I figured I could do something like this: values = [f(x0) for i in range(n)] but I need to update the…
Brais Romero
  • 25
  • 1
  • 7
1
vote
0 answers

Why this 1-liner doesn't work as its non 1-liner version?

I have the following code: f_classes_memberships = {f_set: -1 for f_set in F_SETS} for theta_set in THETA_SETS.keys(): for omega_set in OMEGA_SETS.keys(): f_classes_memberships[FUZZY_TABLE[theta_set][omega_set]] =…
sandrino
  • 65
  • 1
  • 7
1
vote
1 answer

How to extract month and year from a datetime series and store them into two separate columns?

I have a dataframe with a Date column that has datetime objects in it. I can extract the month and year from the Date column and store them in columns of the respective names using the code below. df["Month"] = df["Date"].dt.month df["Year"] =…
Zero
  • 1,800
  • 1
  • 5
  • 16
1
vote
2 answers

Only if statement and no else statement on 1 line

Question: I know it's possible to just put an if statement on one line but is there a way to do it so that PEP8 doesn't reformat it to be on two lines? I know that you can put it on two lines but that doesn't answer my question about the possibility…
Liam
  • 461
  • 4
  • 27
1 2
3
10 11